irgen.cpp 46.5 KB
Newer Older
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1
// Copyright (c) 2014-2015 Dropbox, Inc.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
2
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
3 4 5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Kevin Modzelewski's avatar
Kevin Modzelewski committed
6
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
7
//    http://www.apache.org/licenses/LICENSE-2.0
Kevin Modzelewski's avatar
Kevin Modzelewski committed
8
//
Kevin Modzelewski's avatar
Kevin Modzelewski committed
9 10 11 12 13 14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16
#include "codegen/irgen.h"

Kevin Modzelewski's avatar
Kevin Modzelewski committed
17 18
#include <cstdio>
#include <iostream>
19
#include <set>
Kevin Modzelewski's avatar
Kevin Modzelewski committed
20
#include <sstream>
21
#include <stdint.h>
Kevin Modzelewski's avatar
Kevin Modzelewski committed
22 23

#include "llvm/Analysis/Passes.h"
24
#include "llvm/IR/DIBuilder.h"
25
#include "llvm/IR/Module.h"
Kevin Modzelewski's avatar
Kevin Modzelewski committed
26
#include "llvm/IR/Verifier.h"
Marius Wachtler's avatar
Marius Wachtler committed
27
#if LLVMREV < 229094
28
#include "llvm/PassManager.h"
Marius Wachtler's avatar
Marius Wachtler committed
29 30 31
#else
#include "llvm/IR/LegacyPassManager.h"
#endif
32 33
#include "llvm/Support/FileSystem.h"
#include "llvm/Target/TargetMachine.h"
34
#include "llvm/Target/TargetSubtargetInfo.h"
Kevin Modzelewski's avatar
Kevin Modzelewski committed
35 36 37
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/Transforms/Scalar.h"

38 39 40
#include "analysis/function_analysis.h"
#include "analysis/scoping_analysis.h"
#include "analysis/type_analysis.h"
Kevin Modzelewski's avatar
Kevin Modzelewski committed
41 42
#include "codegen/codegen.h"
#include "codegen/compvars.h"
43
#include "codegen/gcbuilder.h"
44
#include "codegen/irgen/irgenerator.h"
Kevin Modzelewski's avatar
Kevin Modzelewski committed
45 46 47 48
#include "codegen/irgen/util.h"
#include "codegen/opt/escape_analysis.h"
#include "codegen/opt/inliner.h"
#include "codegen/opt/passes.h"
49 50 51 52 53 54 55 56
#include "codegen/osrentry.h"
#include "codegen/patchpoints.h"
#include "codegen/stackmaps.h"
#include "core/ast.h"
#include "core/cfg.h"
#include "core/options.h"
#include "core/stats.h"
#include "core/util.h"
Kevin Modzelewski's avatar
Kevin Modzelewski committed
57
#include "runtime/objmodel.h"
58
#include "runtime/types.h"
59

Kevin Modzelewski's avatar
Kevin Modzelewski committed
60 61 62 63 64 65
namespace pyston {

typedef std::unordered_set<CFGBlock*> BlockSet;

// This is where you can add a hook for any instruction added through the IRBuilder.
// It's currently not doing any hooking; hopefully there's not too much overhead from this.
66 67
void MyInserter::InsertHelper(llvm::Instruction* I, const llvm::Twine& Name, llvm::BasicBlock* BB,
                              llvm::BasicBlock::iterator InsertPt) const {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
68 69 70
    llvm::IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
}

71
static void optimizeIR(llvm::Function* f, EffortLevel effort) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
72 73 74 75 76 77 78
    // TODO maybe should do some simple passes (ex: gvn?) if effort level isn't maximal?
    // In general, this function needs a lot of tuning.
    if (effort < EffortLevel::MAXIMAL)
        return;

    Timer _t("optimizing");

Marius Wachtler's avatar
Marius Wachtler committed
79
#if LLVMREV < 229094
Kevin Modzelewski's avatar
Kevin Modzelewski committed
80
    llvm::FunctionPassManager fpm(g.cur_module);
Marius Wachtler's avatar
Marius Wachtler committed
81 82 83 84
#else
    llvm::legacy::FunctionPassManager fpm(g.cur_module);
#endif

Kevin Modzelewski's avatar
Kevin Modzelewski committed
85

86
#if LLVMREV < 217548
87
    fpm.add(new llvm::DataLayoutPass(*g.tm->getDataLayout()));
88 89 90
#else
    fpm.add(new llvm::DataLayoutPass());
#endif
Kevin Modzelewski's avatar
Kevin Modzelewski committed
91

92 93 94 95 96
    if (ENABLE_PYSTON_PASSES) {
        fpm.add(createRemoveUnnecessaryBoxingPass());
        fpm.add(createRemoveDuplicateBoxingPass());
    }

97 98
    if (ENABLE_INLINING && effort >= EffortLevel::MAXIMAL)
        fpm.add(makeFPInliner(275));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
99 100 101 102 103 104 105 106 107
    fpm.add(llvm::createCFGSimplificationPass());

    fpm.add(llvm::createBasicAliasAnalysisPass());
    fpm.add(llvm::createTypeBasedAliasAnalysisPass());
    if (ENABLE_PYSTON_PASSES) {
        fpm.add(new EscapeAnalysis());
        fpm.add(createPystonAAPass());
    }

108 109
    if (ENABLE_PYSTON_PASSES)
        fpm.add(createMallocsNonNullPass());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    // TODO Find the right place for this pass (and ideally not duplicate it)
    if (ENABLE_PYSTON_PASSES) {
        fpm.add(llvm::createGVNPass());
        fpm.add(createConstClassesPass());
    }

    // TODO: find the right set of passes
    if (0) {
        // My original set of passes, that seem to get about 90% of the benefit:
        fpm.add(llvm::createInstructionCombiningPass());
        fpm.add(llvm::createReassociatePass());
        fpm.add(llvm::createGVNPass());
        fpm.add(llvm::createCFGSimplificationPass());
    } else {
        // copied + slightly modified from llvm/lib/Transforms/IPO/PassManagerBuilder.cpp::populateModulePassManager
126 127
        fpm.add(llvm::createEarlyCSEPass());                   // Catch trivial redundancies
        fpm.add(llvm::createJumpThreadingPass());              // Thread jumps.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
128
        fpm.add(llvm::createCorrelatedValuePropagationPass()); // Propagate conditionals
129 130 131 132 133 134 135 136
        fpm.add(llvm::createCFGSimplificationPass());          // Merge & remove BBs
        fpm.add(llvm::createInstructionCombiningPass());       // Combine silly seq's

        fpm.add(llvm::createTailCallEliminationPass()); // Eliminate tail calls
        fpm.add(llvm::createCFGSimplificationPass());   // Merge & remove BBs
        fpm.add(llvm::createReassociatePass());         // Reassociate expressions
        fpm.add(llvm::createLoopRotatePass());          // Rotate Loop
        fpm.add(llvm::createLICMPass());                // Hoist loop invariants
Kevin Modzelewski's avatar
Kevin Modzelewski committed
137 138
        fpm.add(llvm::createLoopUnswitchPass(true /*optimize_for_size*/));
        fpm.add(llvm::createInstructionCombiningPass());
139 140 141
        fpm.add(llvm::createIndVarSimplifyPass()); // Canonicalize indvars
        fpm.add(llvm::createLoopIdiomPass());      // Recognize idioms like memset.
        fpm.add(llvm::createLoopDeletionPass());   // Delete dead loops
Kevin Modzelewski's avatar
Kevin Modzelewski committed
142

143
        fpm.add(llvm::createLoopUnrollPass()); // Unroll small loops
Kevin Modzelewski's avatar
Kevin Modzelewski committed
144

145 146 147
        fpm.add(llvm::createGVNPass());       // Remove redundancies
        fpm.add(llvm::createMemCpyOptPass()); // Remove memcpy / form memset
        fpm.add(llvm::createSCCPPass());      // Constant prop with SCCP
Kevin Modzelewski's avatar
Kevin Modzelewski committed
148 149 150 151

        // Run instcombine after redundancy elimination to exploit opportunities
        // opened up by them.
        fpm.add(llvm::createInstructionCombiningPass());
152
        fpm.add(llvm::createJumpThreadingPass()); // Thread jumps
Kevin Modzelewski's avatar
Kevin Modzelewski committed
153
        fpm.add(llvm::createCorrelatedValuePropagationPass());
154
        fpm.add(llvm::createDeadStoreEliminationPass()); // Delete dead stores
Kevin Modzelewski's avatar
Kevin Modzelewski committed
155 156

        fpm.add(llvm::createLoopRerollPass());
157
        // fpm.add(llvm::createSLPVectorizerPass());   // Vectorize parallel scalar chains.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
158 159


160 161 162
        fpm.add(llvm::createAggressiveDCEPass());        // Delete dead instructions
        fpm.add(llvm::createCFGSimplificationPass());    // Merge & remove BBs
        fpm.add(llvm::createInstructionCombiningPass()); // Clean up after everything.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
163

164 165
        // fpm.add(llvm::createBarrierNoopPass());
        // fpm.add(llvm::createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
166 167 168 169 170 171 172 173 174 175 176
        fpm.add(llvm::createInstructionCombiningPass());
        fpm.add(llvm::createCFGSimplificationPass());
    }

    // TODO Find the right place for this pass (and ideally not duplicate it)
    if (ENABLE_PYSTON_PASSES) {
        fpm.add(createConstClassesPass());
        fpm.add(llvm::createInstructionCombiningPass());
        fpm.add(llvm::createCFGSimplificationPass());
        fpm.add(createConstClassesPass());
        fpm.add(createDeadAllocsPass());
177 178 179 180
        // fpm.add(llvm::createSCCPPass());                  // Constant prop with SCCP
        // fpm.add(llvm::createEarlyCSEPass());              // Catch trivial redundancies
        // fpm.add(llvm::createInstructionCombiningPass());
        // fpm.add(llvm::createCFGSimplificationPass());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
181 182 183 184 185 186 187 188
    }

    fpm.doInitialization();

    for (int i = 0; i < MAX_OPT_ITERATIONS; i++) {
        bool changed = fpm.run(*f);

        if (!changed) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
189
            if (VERBOSITY("irgen") >= 2)
190
                printf("done after %d optimization iterations\n", i - 1);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
191 192 193
            break;
        }

Kevin Modzelewski's avatar
Kevin Modzelewski committed
194
        if (VERBOSITY("irgen") >= 2) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
195 196 197 198
            fprintf(stderr, "after optimization %d:\n", i);
            printf("\033[36m");
            fflush(stdout);
            dumpPrettyIR(f);
199 200
            // f->dump();
            // g.cur_module->dump();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
201 202 203 204 205 206 207 208 209 210
            printf("\033[0m");
            fflush(stdout);
        }
    }

    long us = _t.end();
    static StatCounter us_optimizing("us_compiling_optimizing");
    us_optimizing.log(us);
}

211
static bool compareBlockPairs(const std::pair<CFGBlock*, CFGBlock*>& p1, const std::pair<CFGBlock*, CFGBlock*>& p2) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
212 213 214
    return p1.first->idx < p2.first->idx;
}

215 216
static std::vector<std::pair<CFGBlock*, CFGBlock*>> computeBlockTraversalOrder(const BlockSet& blocks,
                                                                               CFGBlock* start) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
217

218
    std::vector<std::pair<CFGBlock*, CFGBlock*>> rtn;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
219 220 221
    std::unordered_set<CFGBlock*> in_queue;

    if (start) {
222
        assert(blocks.count(start));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
223 224 225 226 227 228 229 230 231
        in_queue.insert(start);
        rtn.push_back(std::make_pair(start, (CFGBlock*)NULL));
    }

    // It's important for debugging purposes that the order is deterministic, but the iteration
    // over the BlockSet is not:
    std::sort(rtn.begin(), rtn.end(), compareBlockPairs);

    int idx = 0;
232
    while (rtn.size() < blocks.size()) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
233 234 235 236 237
        // TODO: come up with an alternative algorithm that outputs
        // the blocks in "as close to in-order as possible".
        // Do this by iterating over all blocks and picking the smallest one
        // that has a predecessor in the list already.
        while (idx < rtn.size()) {
238
            CFGBlock* cur = rtn[idx].first;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
239 240

            for (int i = 0; i < cur->successors.size(); i++) {
241
                CFGBlock* b = cur->successors[i];
242
                assert(blocks.count(b));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
243 244 245 246 247 248 249 250 251 252
                if (in_queue.count(b))
                    continue;

                rtn.push_back(std::make_pair(b, cur));
                in_queue.insert(b);
            }

            idx++;
        }

253
        if (rtn.size() == blocks.size())
Kevin Modzelewski's avatar
Kevin Modzelewski committed
254 255
            break;

256
        CFGBlock* best = NULL;
257
        for (CFGBlock* b : blocks) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
258 259 260 261 262 263 264 265 266 267 268 269
            if (in_queue.count(b))
                continue;

            // Avoid picking any blocks where we can't add an epilogue to the predecessors
            if (b->predecessors.size() == 1 && b->predecessors[0]->successors.size() > 1)
                continue;

            if (best == NULL || b->idx < best->idx)
                best = b;
        }
        assert(best != NULL);

Kevin Modzelewski's avatar
Kevin Modzelewski committed
270
        if (VERBOSITY("irgen") >= 2)
271
            printf("Giving up and adding block %d to the order\n", best->idx);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
272 273 274 275
        in_queue.insert(best);
        rtn.push_back(std::make_pair(best, (CFGBlock*)NULL));
    }

276
    ASSERT(rtn.size() == blocks.size(), "%ld\n", rtn.size());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
277 278 279
    return rtn;
}

280 281
static ConcreteCompilerType* getTypeAtBlockStart(TypeAnalysis* types, InternedString name, CFGBlock* block) {
    if (isIsDefinedName(name.str()))
282
        return BOOL;
283
    else if (name.str() == PASSED_GENERATOR_NAME)
284
        return GENERATOR;
285
    else if (name.str() == PASSED_CLOSURE_NAME)
286
        return CLOSURE;
287
    else if (name.str() == CREATED_CLOSURE_NAME)
Kevin Modzelewski's avatar
Kevin Modzelewski committed
288
        return CLOSURE;
289 290 291 292
    else
        return types->getTypeAtBlockStart(name, block);
}

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
llvm::Value* handlePotentiallyUndefined(ConcreteCompilerVariable* is_defined_var, llvm::Type* rtn_type,
                                        llvm::BasicBlock*& cur_block, IREmitter& emitter, bool speculate_undefined,
                                        std::function<llvm::Value*(IREmitter&)> when_defined,
                                        std::function<llvm::Value*(IREmitter&)> when_undefined) {
    if (!is_defined_var)
        return when_defined(emitter);

    assert(is_defined_var->getType() == BOOL);
    llvm::Value* is_defined_i1 = i1FromBool(emitter, is_defined_var);

    llvm::BasicBlock* ifdefined_block = emitter.createBasicBlock();
    ifdefined_block->moveAfter(cur_block);
    llvm::BasicBlock* join_block = emitter.createBasicBlock();
    join_block->moveAfter(ifdefined_block);
    llvm::BasicBlock* undefined_block;

    llvm::Value* val_if_undefined;
    if (speculate_undefined) {
        val_if_undefined = when_undefined(emitter);
        undefined_block = cur_block;
        emitter.getBuilder()->CreateCondBr(is_defined_i1, ifdefined_block, join_block);
    } else {
        undefined_block = emitter.createBasicBlock();
        undefined_block->moveAfter(cur_block);
        emitter.getBuilder()->CreateCondBr(is_defined_i1, ifdefined_block, undefined_block);

        cur_block = undefined_block;
        emitter.getBuilder()->SetInsertPoint(undefined_block);
        val_if_undefined = when_undefined(emitter);
        emitter.getBuilder()->CreateBr(join_block);
    }

    cur_block = ifdefined_block;
    emitter.getBuilder()->SetInsertPoint(ifdefined_block);
    llvm::Value* val_if_defined = when_defined(emitter);
    emitter.getBuilder()->CreateBr(join_block);

    cur_block = join_block;
    emitter.getBuilder()->SetInsertPoint(join_block);
    auto phi = emitter.getBuilder()->CreatePHI(rtn_type, 2);
    phi->addIncoming(val_if_undefined, undefined_block);
    phi->addIncoming(val_if_defined, ifdefined_block);
    return phi;
}

338
static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDescriptor* entry_descriptor,
339
                    const BlockSet& blocks) {
340
    SourceInfo* source = irstate->getSourceInfo();
341
    EffortLevel effort = irstate->getEffortLevel();
342 343 344
    CompiledFunction* cf = irstate->getCurFunction();
    ConcreteCompilerType* rtn_type = irstate->getReturnType();
    // llvm::MDNode* func_info = irstate->getFuncDbgInfo();
345
    PhiAnalysis* phi_analysis = irstate->getPhis();
346
    assert(phi_analysis);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
347 348

    if (entry_descriptor != NULL)
349
        assert(blocks.count(source->cfg->getStartingBlock()) == 0);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
350 351

    // We need the entry blocks pre-allocated so that we can jump forward to them.
352 353
    std::unordered_map<CFGBlock*, llvm::BasicBlock*> llvm_entry_blocks;
    for (CFGBlock* block : source->cfg->blocks) {
354
        if (blocks.count(block) == 0) {
355
            llvm_entry_blocks[block] = NULL;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
356 357 358 359
            continue;
        }

        char buf[40];
360
        snprintf(buf, 40, "block%d", block->idx);
361
        llvm_entry_blocks[block] = llvm::BasicBlock::Create(g.context, buf, irstate->getLLVMFunction());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
362 363
    }

364 365
    // the function entry block, where we add the type guards [no guards anymore]
    llvm::BasicBlock* osr_entry_block = NULL;
366 367
    llvm::BasicBlock* osr_unbox_block_end = NULL; // the block after type guards where we up/down-convert things
    ConcreteSymbolTable* osr_syms = NULL;         // syms after conversion
Kevin Modzelewski's avatar
Kevin Modzelewski committed
368
    if (entry_descriptor != NULL) {
369 370
        llvm::BasicBlock* osr_unbox_block = llvm::BasicBlock::Create(g.context, "osr_unbox", irstate->getLLVMFunction(),
                                                                     &irstate->getLLVMFunction()->getEntryBlock());
371 372
        osr_entry_block = llvm::BasicBlock::Create(g.context, "osr_entry", irstate->getLLVMFunction(),
                                                   &irstate->getLLVMFunction()->getEntryBlock());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
373 374 375
        assert(&irstate->getLLVMFunction()->getEntryBlock() == osr_entry_block);

        osr_syms = new ConcreteSymbolTable();
376 377
        SymbolTable* initial_syms = new SymbolTable();
        // llvm::BranchInst::Create(llvm_entry_blocks[entry_descriptor->backedge->target->idx], entry_block);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
378

379
        llvm::BasicBlock* osr_entry_block_end = osr_entry_block;
380
        osr_unbox_block_end = osr_unbox_block;
381
        std::unique_ptr<IREmitter> entry_emitter(createIREmitter(irstate, osr_entry_block_end));
382
        std::unique_ptr<IREmitter> unbox_emitter(createIREmitter(irstate, osr_unbox_block_end));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
383

384
        CFGBlock* target_block = entry_descriptor->backedge->target;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
385 386

        std::vector<llvm::Value*> func_args;
387 388
        for (llvm::Function::arg_iterator AI = irstate->getLLVMFunction()->arg_begin();
             AI != irstate->getLLVMFunction()->arg_end(); AI++) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
389 390 391 392
            func_args.push_back(AI);
        }

        // Handle loading symbols from the passed osr arguments:
Kevin Modzelewski's avatar
Kevin Modzelewski committed
393
        int arg_num = -1;
394
        for (const auto& p : entry_descriptor->args) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
395
            llvm::Value* from_arg;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
396 397 398
            arg_num++;
            if (arg_num < 3) {
                from_arg = func_args[arg_num];
399 400 401 402 403 404 405 406
#ifndef NDEBUG
                if (from_arg->getType() != p.second->llvmType()) {
                    from_arg->getType()->dump();
                    printf("\n");
                    p.second->llvmType()->dump();
                    printf("\n");
                }
#endif
407
                assert(from_arg->getType() == p.second->llvmType());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
408 409
            } else {
                ASSERT(func_args.size() == 4, "%ld", func_args.size());
410
                llvm::Value* ptr = entry_emitter->getBuilder()->CreateConstGEP1_32(func_args[3], arg_num - 3);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
411
                if (p.second == INT) {
412
                    ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.i64->getPointerTo());
413
                } else if (p.second == BOOL) {
414
                    ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, BOOL->llvmType()->getPointerTo());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
415
                } else if (p.second == FLOAT) {
416
                    ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.double_->getPointerTo());
417 418
                } else if (p.second == GENERATOR) {
                    ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.llvm_generator_type_ptr->getPointerTo());
419 420
                } else if (p.second == CLOSURE) {
                    ptr = entry_emitter->getBuilder()->CreateBitCast(ptr, g.llvm_closure_type_ptr->getPointerTo());
Travis Hance's avatar
exec  
Travis Hance committed
421 422 423
                } else if (p.second == FRAME_INFO) {
                    ptr = entry_emitter->getBuilder()->CreateBitCast(
                        ptr, g.llvm_frame_info_type->getPointerTo()->getPointerTo());
424 425
                } else {
                    assert(p.second->llvmType() == g.llvm_value_type_ptr);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
426
                }
427
                from_arg = entry_emitter->getBuilder()->CreateLoad(ptr);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
428
                assert(from_arg->getType() == p.second->llvmType());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
429 430
            }

Travis Hance's avatar
exec  
Travis Hance committed
431 432 433 434 435 436 437
            if (from_arg->getType() == g.llvm_frame_info_type->getPointerTo()) {
                assert(p.first.str() == FRAME_INFO_PTR_NAME);
                irstate->setFrameInfoArgument(from_arg);
                // Don't add the frame info to the symbol table since we will store it separately:
                continue;
            }

438
            ConcreteCompilerType* phi_type;
439
            phi_type = getTypeAtBlockStart(types, p.first, target_block);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
440

441
            ConcreteCompilerVariable* var = new ConcreteCompilerVariable(p.second, from_arg, true);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
442
            (*initial_syms)[p.first] = var;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
443 444 445 446 447

            // It's possible to OSR into a version of the function with a higher speculation level;
            // this means that the types of the OSR variables are potentially higher (more unspecialized)
            // than what the optimized code expects.
            // So, we have to re-check the speculations and potentially deopt.
448
            llvm::Value* v = NULL;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
449
            if (p.second == phi_type) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
450 451
                // good to go
                v = from_arg;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
452
            } else if (p.second->canConvertTo(phi_type)) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
453 454
                // not sure if/when this happens, but if there's a type mismatch but one we know
                // can be handled (such as casting from a subclass to a superclass), handle it:
455
                ConcreteCompilerVariable* converted = var->makeConverted(*unbox_emitter, phi_type);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
456 457 458
                v = converted->getValue();
                delete converted;
            } else {
459 460
                RELEASE_ASSERT(0, "OSR'd with a %s into a type inference of a %s?\n", p.second->debugName().c_str(),
                               phi_type->debugName().c_str());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
461 462
            }

Kevin Modzelewski's avatar
Kevin Modzelewski committed
463
            if (VERBOSITY("irgen") >= 2)
464
                v->setName("prev_" + p.first.str());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
465

Kevin Modzelewski's avatar
Kevin Modzelewski committed
466
            (*osr_syms)[p.first] = new ConcreteCompilerVariable(phi_type, v, true);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
467 468
        }

469
        entry_emitter->getBuilder()->CreateBr(osr_unbox_block);
470
        unbox_emitter->getBuilder()->CreateBr(llvm_entry_blocks[entry_descriptor->backedge->target]);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
471

472
        for (const auto& p : *initial_syms) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
473
            delete p.second;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
474 475 476 477 478 479 480 481
        }
        delete initial_syms;
    }

    // In a similar vein, we need to keep track of the exit blocks for each cfg block,
    // so that we can construct phi nodes later.
    // Originally I preallocated these blocks as well, but we can construct the phi's
    // after the fact, so we can just record the exit blocks as we go along.
482
    std::unordered_map<CFGBlock*, llvm::BasicBlock*> llvm_exit_blocks;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
483 484 485 486

    ////
    // Main ir generation: go through each basic block in the CFG and emit the code

487 488
    std::unordered_map<CFGBlock*, SymbolTable*> ending_symbol_tables;
    std::unordered_map<CFGBlock*, ConcreteSymbolTable*> phi_ending_symbol_tables;
489
    typedef std::unordered_map<InternedString, std::pair<ConcreteCompilerType*, llvm::PHINode*>> PHITable;
490
    std::unordered_map<CFGBlock*, PHITable*> created_phis;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
491 492 493 494

    CFGBlock* initial_block = NULL;
    if (entry_descriptor) {
        initial_block = entry_descriptor->backedge->target;
495
    } else if (blocks.count(source->cfg->getStartingBlock())) {
496
        initial_block = source->cfg->getStartingBlock();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
497 498 499 500 501 502 503 504 505
    }

    // The rest of this code assumes that for each non-entry block that gets evaluated,
    // at least one of its predecessors has been evaluated already (from which it will
    // get type information).
    // The cfg generation code will generate a cfg such that each block has a predecessor
    // with a lower index value, so if the entry block is 0 then we can iterate in index
    // order.
    // The entry block doesn't have to be zero, so we have to calculate an allowable order here:
506
    std::vector<std::pair<CFGBlock*, CFGBlock*>> traversal_order = computeBlockTraversalOrder(blocks, initial_block);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
507 508 509

    std::unordered_set<CFGBlock*> into_hax;
    for (int _i = 0; _i < traversal_order.size(); _i++) {
510 511
        CFGBlock* block = traversal_order[_i].first;
        CFGBlock* pred = traversal_order[_i].second;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
512

513
        if (!blocks.count(block))
Kevin Modzelewski's avatar
Kevin Modzelewski committed
514
            continue;
515 516 517

        if (VERBOSITY("irgen") >= 2)
            printf("processing block %d\n", block->idx);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
518

519
        std::unique_ptr<IRGenerator> generator(createIRGenerator(irstate, llvm_entry_blocks, block, types));
520 521
        llvm::BasicBlock* entry_block_end = llvm_entry_blocks[block];
        std::unique_ptr<IREmitter> emitter(createIREmitter(irstate, entry_block_end));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
522

523 524
        PHITable* phis = new PHITable();
        created_phis[block] = phis;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
525 526

        // Set initial symbol table:
527 528
        // If we're in the starting block, no phis or symbol table changes for us.
        // Generate function entry code instead.
529
        if (block == source->cfg->getStartingBlock()) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
530 531
            assert(entry_descriptor == NULL);

Kevin Modzelewski's avatar
Kevin Modzelewski committed
532
            if (ENABLE_REOPT && effort < EffortLevel::MAXIMAL && source->ast != NULL
533
                && source->ast->type != AST_TYPE::Module) {
534 535 536
                llvm::BasicBlock* preentry_bb
                    = llvm::BasicBlock::Create(g.context, "pre_entry", irstate->getLLVMFunction(),
                                               llvm_entry_blocks[source->cfg->getStartingBlock()]);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
537
                llvm::BasicBlock* reopt_bb = llvm::BasicBlock::Create(g.context, "reopt", irstate->getLLVMFunction());
538
                emitter->getBuilder()->SetInsertPoint(preentry_bb);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
539

540
                llvm::Value* call_count_ptr = embedRelocatablePtr(&cf->times_called, g.i64->getPointerTo());
541 542 543
                llvm::Value* cur_call_count = emitter->getBuilder()->CreateLoad(call_count_ptr);
                llvm::Value* new_call_count
                    = emitter->getBuilder()->CreateAdd(cur_call_count, getConstantInt(1, g.i64));
544
                emitter->getBuilder()->CreateStore(new_call_count, call_count_ptr);
545

Kevin Modzelewski's avatar
Kevin Modzelewski committed
546 547 548 549 550 551 552 553
                int reopt_threshold;
                if (effort == EffortLevel::MINIMAL)
                    reopt_threshold = REOPT_THRESHOLD_BASELINE;
                else if (effort == EffortLevel::MODERATE)
                    reopt_threshold = REOPT_THRESHOLD_T2;
                else
                    RELEASE_ASSERT(0, "Unknown effort: %d", (int)effort);

554 555
                llvm::Value* reopt_test
                    = emitter->getBuilder()->CreateICmpSGT(new_call_count, getConstantInt(reopt_threshold, g.i64));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
556

Kevin Modzelewski's avatar
Kevin Modzelewski committed
557 558 559 560
                llvm::Metadata* md_vals[] = { llvm::MDString::get(g.context, "branch_weights"),
                                              llvm::ConstantAsMetadata::get(getConstantInt(1)),
                                              llvm::ConstantAsMetadata::get(getConstantInt(1000)) };
                llvm::MDNode* branch_weights = llvm::MDNode::get(g.context, llvm::ArrayRef<llvm::Metadata*>(md_vals));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
561

562 563
                llvm::BranchInst* guard = emitter->getBuilder()->CreateCondBr(
                    reopt_test, reopt_bb, llvm_entry_blocks[source->cfg->getStartingBlock()], branch_weights);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
564

565
                emitter->getBuilder()->SetInsertPoint(reopt_bb);
566 567
                // emitter->getBuilder()->CreateCall(g.funcs.my_assert, getConstantInt(0, g.i1));
                llvm::Value* r = emitter->getBuilder()->CreateCall(g.funcs.reoptCompiledFunc,
568
                                                                   embedRelocatablePtr(cf, g.i8->getPointerTo()));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
569 570 571
                assert(r);
                assert(r->getType() == g.i8->getPointerTo());

572
                llvm::Value* bitcast_r = emitter->getBuilder()->CreateBitCast(r, irstate->getLLVMFunction()->getType());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
573 574

                std::vector<llvm::Value*> args;
575 576 577 578
                // bitcast_r->dump();
                for (llvm::Function::arg_iterator AI = irstate->getLLVMFunction()->arg_begin();
                     AI != irstate->getLLVMFunction()->arg_end(); AI++) {
                    // AI->dump();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
579 580
                    args.push_back(&(*AI));
                }
581 582
                // printf("%ld\n", args.size());
                llvm::CallInst* postcall = emitter->getBuilder()->CreateCall(bitcast_r, args);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
583 584
                postcall->setTailCall(true);
                if (rtn_type == VOID) {
585
                    emitter->getBuilder()->CreateRetVoid();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
586
                } else {
587
                    emitter->getBuilder()->CreateRet(postcall);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
588 589
                }

590
                emitter->getBuilder()->SetInsertPoint(llvm_entry_blocks[source->cfg->getStartingBlock()]);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
591
            }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
592

593
            generator->doFunctionEntry(*irstate->getParamNames(), cf->spec->arg_types);
594 595 596 597

            // Function-entry safepoint:
            // TODO might be more efficient to do post-call safepoints?
            generator->doSafePoint();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
598 599 600 601 602
        } else if (entry_descriptor && block == entry_descriptor->backedge->target) {
            assert(block->predecessors.size() > 1);
            assert(osr_entry_block);
            assert(phis);

603
            for (const auto& p : entry_descriptor->args) {
Travis Hance's avatar
exec  
Travis Hance committed
604 605 606 607 608
                // Don't add the frame info to the symbol table since we will store it separately
                // (we manually added it during the calculation of osr_syms):
                if (p.first.str() == FRAME_INFO_PTR_NAME)
                    continue;

609
                ConcreteCompilerType* analyzed_type = getTypeAtBlockStart(types, p.first, block);
610

611
                // printf("For %s, given %s, analyzed for %s\n", p.first.c_str(), p.second->debugName().c_str(),
612
                //        analyzed_type->debugName().c_str());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
613

614
                llvm::PHINode* phi = emitter->getBuilder()->CreatePHI(analyzed_type->llvmType(),
615
                                                                      block->predecessors.size() + 1, p.first.str());
616
                ConcreteCompilerVariable* var = new ConcreteCompilerVariable(analyzed_type, phi, true);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
617 618
                generator->giveLocalSymbol(p.first, var);
                (*phis)[p.first] = std::make_pair(analyzed_type, phi);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
619 620 621 622 623 624
            }
        } else if (pred == NULL) {
            assert(traversal_order.size() < source->cfg->blocks.size());
            assert(phis);
            assert(block->predecessors.size());
            for (int i = 0; i < block->predecessors.size(); i++) {
625
                CFGBlock* b2 = block->predecessors[i];
626
                assert(ending_symbol_tables.count(b2) == 0);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
627 628 629
                into_hax.insert(b2);
            }

630

631
            std::set<InternedString> names;
632
            for (const auto& s : phi_analysis->getAllRequiredFor(block)) {
633
                names.insert(s);
634
                if (phi_analysis->isPotentiallyUndefinedAfter(s, block->predecessors[0])) {
635
                    names.insert(getIsDefinedName(s, source->getInternedStrings()));
636 637 638 639
                }
            }

            if (source->getScopeInfo()->createsClosure())
640
                names.insert(source->getInternedStrings().get(CREATED_CLOSURE_NAME));
641 642

            if (source->getScopeInfo()->takesClosure())
643
                names.insert(source->getInternedStrings().get(PASSED_CLOSURE_NAME));
644

645
            if (source->is_generator)
646
                names.insert(source->getInternedStrings().get(PASSED_GENERATOR_NAME));
647

Travis Hance's avatar
exec  
Travis Hance committed
648
            for (const InternedString& s : names) {
649
                // printf("adding guessed phi for %s\n", s.c_str());
650
                ConcreteCompilerType* type = getTypeAtBlockStart(types, s, block);
651 652
                llvm::PHINode* phi
                    = emitter->getBuilder()->CreatePHI(type->llvmType(), block->predecessors.size(), s.str());
653
                ConcreteCompilerVariable* var = new ConcreteCompilerVariable(type, phi, true);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
654
                generator->giveLocalSymbol(s, var);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
655

Kevin Modzelewski's avatar
Kevin Modzelewski committed
656
                (*phis)[s] = std::make_pair(type, phi);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
657 658 659
            }
        } else {
            assert(pred);
660
            assert(blocks.count(pred));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
661

662
            if (block->predecessors.size() == 1) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
663 664
                // If this block has only one predecessor, it by definition doesn't need any phi nodes.
                // Assert that the phi_st is empty, and just create the symbol table from the non-phi st:
665 666
                ASSERT(phi_ending_symbol_tables[pred]->size() == 0, "%d %d", block->idx, pred->idx);
                assert(ending_symbol_tables.count(pred));
667

668 669 670 671 672
                // Filter out any names set by an invoke statement at the end of the previous block, if we're in the
                // unwind path. This definitely doesn't seem like the most elegant way to do this, but the rest of the
                // analysis frameworks can't (yet) support the idea of a block flowing differently to its different
                // successors.
                //
673
                // There are four kinds of AST statements which can set a name:
674 675 676
                // - Assign
                // - ClassDef
                // - FunctionDef
677 678 679 680 681
                // - Import, ImportFrom
                //
                // However, all of these get translated away into Assigns, so we only need to worry about those. Also,
                // as an invariant, all assigns that can fail assign to a temporary rather than a python name. This
                // ensures that we interoperate properly with definedness analysis.
682 683 684 685 686 687
                //
                // We only need to do this in the case that we have exactly one predecessor, because:
                // - a block ending in an invoke will have multiple successors
                // - critical edges (block with multiple successors -> block with multiple predecessors)
                //   are disallowed

688 689 690 691 692
                auto pred = block->predecessors[0];
                auto last_inst = pred->body.back();

                SymbolTable* sym_table = ending_symbol_tables[pred];
                bool created_new_sym_table = false;
693 694
                if (last_inst->type == AST_TYPE::Invoke && ast_cast<AST_Invoke>(last_inst)->exc_dest == block) {
                    AST_stmt* stmt = ast_cast<AST_Invoke>(last_inst)->stmt;
695 696 697 698 699 700 701

                    // The CFG pass translates away these statements, so we should never encounter them.
                    // If we did, we'd need to remove a name here.
                    assert(stmt->type != AST_TYPE::ClassDef);
                    assert(stmt->type != AST_TYPE::FunctionDef);
                    assert(stmt->type != AST_TYPE::Import);
                    assert(stmt->type != AST_TYPE::ImportFrom);
702 703 704

                    if (stmt->type == AST_TYPE::Assign) {
                        auto asgn = ast_cast<AST_Assign>(stmt);
705 706
                        assert(asgn->targets.size() == 1);
                        if (asgn->targets[0]->type == AST_TYPE::Name) {
707 708
                            InternedString name = ast_cast<AST_Name>(asgn->targets[0])->id;
                            assert(name.c_str()[0] == '#'); // it must be a temporary
709 710 711
                            // You might think I need to check whether `name' is being assigned globally or locally,
                            // since a global assign doesn't affect the symbol table. However, the CFG pass only
                            // generates invoke-assigns to temporary variables. Just to be sure, we assert:
712
                            assert(source->getScopeInfo()->getScopeTypeOfName(name) != ScopeInfo::VarScopeType::GLOBAL);
713

714
                            // TODO: inefficient
715
                            sym_table = new SymbolTable(*sym_table);
716 717
                            ASSERT(sym_table->count(name), "%d %s\n", block->idx, name.c_str());
                            sym_table->erase(name);
718 719 720 721 722 723 724 725
                            created_new_sym_table = true;
                        }
                    }
                }

                generator->copySymbolsFrom(sym_table);
                if (created_new_sym_table)
                    delete sym_table;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
726
            } else {
727 728
                // With multiple predecessors, the symbol tables at the end of each predecessor should be *exactly* the
                // same.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
729 730 731 732
                // (this should be satisfied by the post-run() code in this function)

                // With multiple predecessors, we have to combine the non-phi and phi symbol tables.
                // Start off with the non-phi ones:
733
                generator->copySymbolsFrom(ending_symbol_tables[pred]);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
734

735
                // NB. This is where most `typical' phi nodes get added.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
736
                // And go through and add phi nodes:
737
                ConcreteSymbolTable* pred_st = phi_ending_symbol_tables[pred];
738

739 740 741 742 743 744 745 746 747 748
                // We have to sort the phi table by name in order to a get a deterministic ordering for the JIT object
                // cache.
                typedef std::pair<InternedString, ConcreteCompilerVariable*> Entry;
                std::vector<Entry> sorted_pred_st(pred_st->begin(), pred_st->end());
                std::sort(sorted_pred_st.begin(), sorted_pred_st.end(),
                          [](const Entry& lhs, const Entry& rhs) { return lhs.first < rhs.first; });

                for (auto& entry : sorted_pred_st) {
                    InternedString name = entry.first;
                    ConcreteCompilerVariable* cv = entry.second; // incoming CCV from predecessor block
749 750 751
                    // printf("block %d: adding phi for %s from pred %d\n", block->idx, name.c_str(), pred->idx);
                    llvm::PHINode* phi = emitter->getBuilder()->CreatePHI(cv->getType()->llvmType(),
                                                                          block->predecessors.size(), name.str());
752
                    // emitter->getBuilder()->CreateCall(g.funcs.dump, phi);
753 754
                    ConcreteCompilerVariable* var = new ConcreteCompilerVariable(cv->getType(), phi, true);
                    generator->giveLocalSymbol(name, var);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
755

756
                    (*phis)[name] = std::make_pair(cv->getType(), phi);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
757 758 759 760
                }
            }
        }

761
        // Generate loop safepoints on backedges.
762 763 764 765 766 767 768 769 770
        for (CFGBlock* predecessor : block->predecessors) {
            if (predecessor->idx > block->idx) {
                // Loop safepoint:
                // TODO does it matter which side of the backedge these are on?
                generator->doSafePoint();
                break;
            }
        }

771
        // Generate the IR for the block.
772
        generator->run(block);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
773

774
        const IRGenerator::EndingState& ending_st = generator->getEndingSymbolTable();
775 776 777
        ending_symbol_tables[block] = ending_st.symbol_table;
        phi_ending_symbol_tables[block] = ending_st.phi_symbol_table;
        llvm_exit_blocks[block] = ending_st.ending_block;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
778 779 780 781 782 783

        if (into_hax.count(block))
            ASSERT(ending_st.symbol_table->size() == 0, "%d", block->idx);
    }

    ////
784
    // Phi population.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
785 786 787
    // We don't know the exact ssa values to back-propagate to the phi nodes until we've generated
    // the relevant IR, so after we have done all of it, go back through and populate the phi nodes.
    // Also, do some checking to make sure that the phi analysis stuff worked out, and that all blocks
788
    // agreed on what symbols + types they should be propagating for the phis.
789 790
    for (CFGBlock* b : source->cfg->blocks) {
        PHITable* phis = created_phis[b];
Kevin Modzelewski's avatar
Kevin Modzelewski committed
791 792 793
        if (phis == NULL)
            continue;

794
        bool this_is_osr_entry = (entry_descriptor && b == entry_descriptor->backedge->target);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
795

796 797
#ifndef NDEBUG
        // Check to see that all blocks agree on what symbols + types they should be propagating for phis.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
798
        for (int j = 0; j < b->predecessors.size(); j++) {
799 800
            CFGBlock* bpred = b->predecessors[j];
            if (blocks.count(bpred) == 0)
Kevin Modzelewski's avatar
Kevin Modzelewski committed
801 802
                continue;

803 804
            // printf("(%d %ld) -> (%d %ld)\n", bpred->idx, phi_ending_symbol_tables[bpred]->size(), b->idx,
            // phis->size());
805
            ASSERT(sameKeyset(phi_ending_symbol_tables[bpred], phis), "%d->%d", bpred->idx, b->idx);
806
            assert(phi_ending_symbol_tables[bpred]->size() == phis->size());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
807 808 809
        }

        if (this_is_osr_entry) {
810
            assert(sameKeyset(osr_syms, phis));
Kevin Modzelewski's avatar
Kevin Modzelewski committed
811
        }
812
#endif // end checking phi agreement.
Kevin Modzelewski's avatar
Kevin Modzelewski committed
813

814 815 816 817
        // Can't always add the phi incoming value right away, since we may have to create more
        // basic blocks as part of type coercion.
        // Intsead, just make a record of the phi node, value, and the location of the from-BB,
        // which we won't read until after all new BBs have been added.
818
        std::vector<std::tuple<llvm::PHINode*, llvm::Value*, llvm::BasicBlock*&>> phi_args;
819

820
        for (auto it = phis->begin(); it != phis->end(); ++it) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
821 822
            llvm::PHINode* llvm_phi = it->second.second;
            for (int j = 0; j < b->predecessors.size(); j++) {
823 824
                CFGBlock* bpred = b->predecessors[j];
                if (blocks.count(bpred) == 0)
Kevin Modzelewski's avatar
Kevin Modzelewski committed
825 826
                    continue;

827
                ConcreteCompilerVariable* v = (*phi_ending_symbol_tables[bpred])[it->first];
Kevin Modzelewski's avatar
Kevin Modzelewski committed
828 829 830 831
                assert(v);
                assert(v->isGrabbed());

                // Make sure they all prepared for the same type:
832
                ASSERT(it->second.first == v->getType(), "%d %d: %s %s %s", b->idx, bpred->idx, it->first.c_str(),
833
                       it->second.first->debugName().c_str(), v->getType()->debugName().c_str());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
834

835
                llvm::Value* val = v->getValue();
836
                llvm_phi->addIncoming(v->getValue(), llvm_exit_blocks[b->predecessors[j]]);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
837 838 839
            }

            if (this_is_osr_entry) {
840
                ConcreteCompilerVariable* v = (*osr_syms)[it->first];
Kevin Modzelewski's avatar
Kevin Modzelewski committed
841 842 843 844
                assert(v);
                assert(v->isGrabbed());

                ASSERT(it->second.first == v->getType(), "");
845
                llvm_phi->addIncoming(v->getValue(), osr_unbox_block_end);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
846 847
            }
        }
848 849 850
        for (auto t : phi_args) {
            std::get<0>(t)->addIncoming(std::get<1>(t), std::get<2>(t));
        }
Kevin Modzelewski's avatar
Kevin Modzelewski committed
851 852
    }

853
    // deallocate/dereference memory
854
    for (CFGBlock* b : source->cfg->blocks) {
855
        if (ending_symbol_tables[b] == NULL)
Kevin Modzelewski's avatar
Kevin Modzelewski committed
856 857
            continue;

858
        for (SymbolTable::iterator it = ending_symbol_tables[b]->begin(); it != ending_symbol_tables[b]->end(); ++it) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
859 860
            it->second->decvrefNodrop();
        }
861
        for (ConcreteSymbolTable::iterator it = phi_ending_symbol_tables[b]->begin();
862
             it != phi_ending_symbol_tables[b]->end(); ++it) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
863 864
            it->second->decvrefNodrop();
        }
865 866 867
        delete phi_ending_symbol_tables[b];
        delete ending_symbol_tables[b];
        delete created_phis[b];
Kevin Modzelewski's avatar
Kevin Modzelewski committed
868 869 870
    }

    if (entry_descriptor) {
871
        for (const auto& p : *osr_syms) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
872
            delete p.second;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
873 874 875 876 877
        }
        delete osr_syms;
    }
}

878
static void computeBlockSetClosure(BlockSet& blocks) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
879
    if (VERBOSITY("irgen") >= 2) {
880 881
        printf("Initial:");
        for (CFGBlock* b : blocks) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
882
            printf(" %d", b->idx);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
883 884 885 886 887
        }
        printf("\n");
    }
    std::vector<CFGBlock*> q;
    BlockSet expanded;
888
    q.insert(q.end(), blocks.begin(), blocks.end());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
889 890

    while (q.size()) {
891
        CFGBlock* b = q.back();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
892 893 894 895 896 897 898
        q.pop_back();

        if (expanded.count(b))
            continue;
        expanded.insert(b);

        for (int i = 0; i < b->successors.size(); i++) {
899
            CFGBlock* b2 = b->successors[i];
900
            blocks.insert(b2);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
901 902 903 904
            q.push_back(b2);
        }
    }

Kevin Modzelewski's avatar
Kevin Modzelewski committed
905
    if (VERBOSITY("irgen") >= 2) {
906 907
        printf("Ending:");
        for (CFGBlock* b : blocks) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
908
            printf(" %d", b->idx);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
909 910 911 912 913
        }
        printf("\n");
    }
}
// returns a pointer to the function-info mdnode
914
static llvm::MDNode* setupDebugInfo(SourceInfo* source, llvm::Function* f, std::string origname) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
915 916 917 918 919 920
    int lineno = 0;
    if (source->ast)
        lineno = source->ast->lineno;

    llvm::DIBuilder builder(*g.cur_module);

921
    const std::string& fn = source->fn;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
922
    std::string dir = "";
Kevin Modzelewski's avatar
Kevin Modzelewski committed
923 924 925
    std::string producer = "pyston; git rev " STRINGIFY(GITREV);

    llvm::DIFile file = builder.createFile(fn, dir);
926
    llvm::DITypeArray param_types = builder.getOrCreateTypeArray(llvm::None);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
927
    llvm::DICompositeType func_type = builder.createSubroutineType(file, param_types);
928 929
    llvm::DISubprogram func_info = builder.createFunction(file, f->getName(), f->getName(), file, lineno, func_type,
                                                          false, true, lineno + 1, 0, true, f);
930

931 932
    llvm::DICompileUnit compile_unit
        = builder.createCompileUnit(llvm::dwarf::DW_LANG_Python, fn, dir, producer, true, "", 0);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
933

Kevin Modzelewski's avatar
Kevin Modzelewski committed
934
    builder.finalize();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
935 936 937
    return func_info;
}

938
static std::string getUniqueFunctionName(std::string nameprefix, EffortLevel effort, const OSREntryDescriptor* entry) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
939 940 941 942
    static int num_functions = 0;

    std::ostringstream os;
    os << nameprefix;
943
    os << "_e" << (int)effort;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
944
    if (entry) {
Marius Wachtler's avatar
Marius Wachtler committed
945 946 947
        os << "_osr" << entry->backedge->target->idx;
        if (entry->cf->func)
            os << "_from_" << entry->cf->func->getName().data();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
948 949 950 951 952 953
    }
    os << '_' << num_functions;
    num_functions++;
    return os.str();
}

954
CompiledFunction* doCompile(SourceInfo* source, ParamNames* param_names, const OSREntryDescriptor* entry_descriptor,
955
                            EffortLevel effort, FunctionSpecialization* spec, std::string nameprefix) {
956
    Timer _t("in doCompile");
957 958
    Timer _t2;
    long irgen_us = 0;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
959

960 961
    assert((entry_descriptor != NULL) + (spec != NULL) == 1);

962
    if (VERBOSITY("irgen") >= 2)
963
        source->cfg->print();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
964 965

    assert(g.cur_module == NULL);
966 967 968

    clearRelocatableSymsMap();

Kevin Modzelewski's avatar
Kevin Modzelewski committed
969 970
    std::string name = getUniqueFunctionName(nameprefix, effort, entry_descriptor);
    g.cur_module = new llvm::Module(name, g.context);
971
#if LLVMREV < 217070 // not sure if this is the right rev
Kevin Modzelewski's avatar
Kevin Modzelewski committed
972
    g.cur_module->setDataLayout(g.tm->getDataLayout()->getStringRepresentation());
Marius Wachtler's avatar
Marius Wachtler committed
973
#elif LLVMREV < 227113
974
    g.cur_module->setDataLayout(g.tm->getSubtargetImpl()->getDataLayout());
Marius Wachtler's avatar
Marius Wachtler committed
975 976
#else
    g.cur_module->setDataLayout(g.tm->getDataLayout());
977
#endif
978
    // g.engine->addModule(g.cur_module);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
979 980 981 982

    ////
    // Initializing the llvm-level structures:

983

Kevin Modzelewski's avatar
Kevin Modzelewski committed
984
    std::vector<llvm::Type*> llvm_arg_types;
985
    if (entry_descriptor == NULL) {
986 987 988 989 990
        assert(spec);

        int nargs = param_names->totalParameters();
        ASSERT(nargs == spec->arg_types.size(), "%d %ld", nargs, spec->arg_types.size());

991
        if (source->getScopeInfo()->takesClosure())
992
            llvm_arg_types.push_back(g.llvm_closure_type_ptr);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
993

994
        if (source->is_generator)
995
            llvm_arg_types.push_back(g.llvm_generator_type_ptr);
996

Kevin Modzelewski's avatar
Kevin Modzelewski committed
997 998 999 1000 1001
        for (int i = 0; i < nargs; i++) {
            if (i == 3) {
                llvm_arg_types.push_back(g.llvm_value_type_ptr->getPointerTo());
                break;
            }
1002
            llvm_arg_types.push_back(spec->arg_types[i]->llvmType());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1003 1004
        }
    } else {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1005
        int arg_num = -1;
1006
        for (const auto& p : entry_descriptor->args) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1007
            arg_num++;
1008
            // printf("Loading %s: %s\n", p.first.c_str(), p.second->debugName().c_str());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1009 1010
            if (arg_num < 3)
                llvm_arg_types.push_back(p.second->llvmType());
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1011 1012 1013 1014 1015 1016 1017
            else {
                llvm_arg_types.push_back(g.llvm_value_type_ptr->getPointerTo());
                break;
            }
        }
    }

1018 1019

    CompiledFunction* cf
1020
        = new CompiledFunction(NULL, spec, (effort == EffortLevel::INTERPRETED), NULL, effort, entry_descriptor);
1021 1022

    llvm::FunctionType* ft = llvm::FunctionType::get(cf->getReturnType()->llvmType(), llvm_arg_types, false /*vararg*/);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1023

1024
    llvm::Function* f = llvm::Function::Create(ft, llvm::Function::ExternalLinkage, name, g.cur_module);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1025

1026 1027 1028
    cf->func = f;

    // g.func_registry.registerFunction(f, g.cur_module);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1029 1030 1031

    llvm::MDNode* dbg_funcinfo = setupDebugInfo(source, f, nameprefix);

1032 1033
    irgen_us += _t2.split();

Kevin Modzelewski's avatar
Kevin Modzelewski committed
1034
    TypeAnalysis::SpeculationLevel speculation_level = TypeAnalysis::NONE;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1035
    EffortLevel min_speculation_level = EffortLevel::MODERATE;
1036
    if (ENABLE_SPECULATION && effort >= min_speculation_level)
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1037
        speculation_level = TypeAnalysis::SOME;
1038 1039
    TypeAnalysis* types;
    if (entry_descriptor)
1040
        types = doTypeAnalysis(entry_descriptor, effort, speculation_level, source->getScopeInfo());
1041 1042 1043 1044
    else
        types = doTypeAnalysis(source->cfg, *param_names, spec->arg_types, effort, speculation_level,
                               source->getScopeInfo());

Kevin Modzelewski's avatar
Kevin Modzelewski committed
1045

1046 1047
    _t2.split();

1048
    BlockSet blocks;
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1049
    if (entry_descriptor == NULL) {
1050
        for (CFGBlock* b : source->cfg->blocks) {
1051
            blocks.insert(b);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1052 1053
        }
    } else {
1054 1055
        blocks.insert(entry_descriptor->backedge->target);
        computeBlockSetClosure(blocks);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1056 1057
    }

1058 1059 1060 1061 1062 1063 1064 1065 1066
    std::unique_ptr<LivenessAnalysis> liveness = computeLivenessInfo(source->cfg);
    std::unique_ptr<PhiAnalysis> phis;

    if (entry_descriptor)
        phis = computeRequiredPhis(entry_descriptor, liveness.get(), source->getScopeInfo());
    else
        phis = computeRequiredPhis(*param_names, source->cfg, liveness.get(), source->getScopeInfo());

    IRGenState irstate(cf, source, std::move(liveness), std::move(phis), param_names, getGCBuilder(), dbg_funcinfo);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1067

1068
    emitBBs(&irstate, types, entry_descriptor, blocks);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1069 1070 1071 1072 1073

    // De-opt handling:

    delete types;

1074
    if (VERBOSITY("irgen") >= 2) {
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1075 1076 1077 1078
        printf("generated IR:\n");
        printf("\033[33m");
        fflush(stdout);
        dumpPrettyIR(f);
1079 1080 1081
        // f->dump();
        // g.cur_module->dump();
        // g.cur_module->print(llvm::outs(), NULL);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1082 1083 1084 1085
        printf("\033[0m");
        fflush(stdout);
    } else {
        // Somehow, running this code makes it faster...?????
1086 1087
        // printf("\033[0m");
        // fflush(stdout);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1088 1089
    }

1090
    irgen_us += _t2.split();
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1091
    static StatCounter us_irgen("us_compiling_irgen");
1092
    us_irgen.log(irgen_us);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

    if (ENABLE_LLVMOPTS)
        optimizeIR(f, effort);

    g.cur_module = NULL;

    return cf;
}



} // namespace pyston