1 //===- CodeGenLICM.cpp - LICM a function for code generation --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This function performs late LICM, hoisting constants out of loops that
11 // are not valid immediates. It should not be followed by instcombine,
12 // because instcombine would quickly stuff the constants back into the loop.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "codegen-licm"
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Analysis/LoopPass.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/ADT/DenseMap.h"
29 class CodeGenLICM : public LoopPass {
30 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
31 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
33 static char ID; // Pass identification, replacement for typeid
34 explicit CodeGenLICM() : LoopPass(&ID) {}
38 char CodeGenLICM::ID = 0;
39 static RegisterPass<CodeGenLICM> X("codegen-licm",
40 "hoist constants out of loops");
42 Pass *llvm::createCodeGenLICMPass() {
43 return new CodeGenLICM();
46 bool CodeGenLICM::runOnLoop(Loop *L, LPPassManager &) {
49 // Only visit outermost loops.
50 if (L->getParentLoop()) return Changed;
52 Instruction *PreheaderTerm = L->getLoopPreheader()->getTerminator();
53 DenseMap<Constant *, BitCastInst *> HoistedConstants;
55 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
58 for (BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
61 // TODO: For now, skip all intrinsic instructions, because some of them
62 // can require their operands to be constants, and we don't want to
64 if (isa<IntrinsicInst>(I))
66 // LLVM represents fneg as -0.0-x; don't hoist the -0.0 out.
67 if (BinaryOperator::isFNeg(I) ||
68 BinaryOperator::isNeg(I) ||
69 BinaryOperator::isNot(I))
71 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
72 // Don't hoist out switch case constants.
73 if (isa<SwitchInst>(I) && i == 1)
75 // Don't hoist out shuffle masks.
76 if (isa<ShuffleVectorInst>(I) && i == 2)
78 Value *Op = I->getOperand(i);
79 Constant *C = dyn_cast<Constant>(Op);
81 // TODO: Ask the target which constants are legal. This would allow
82 // us to add support for hoisting ConstantInts and GlobalValues too.
83 if (isa<ConstantFP>(C) ||
84 isa<ConstantVector>(C) ||
85 isa<ConstantAggregateZero>(C)) {
86 BitCastInst *&BC = HoistedConstants[C];
88 BC = new BitCastInst(C, C->getType(), "hoist", PreheaderTerm);
99 void CodeGenLICM::getAnalysisUsage(AnalysisUsage &AU) const {
100 // This pass preserves just about everything. List some popular things here.
101 AU.setPreservesCFG();
102 AU.addPreservedID(LoopSimplifyID);
103 AU.addPreserved<LoopInfo>();
104 AU.addPreserved<AliasAnalysis>();
105 AU.addPreserved("scalar-evolution");
106 AU.addPreserved("iv-users");
107 AU.addPreserved("lda");
108 AU.addPreserved("live-values");
110 // Hoisting requires a loop preheader.
111 AU.addRequiredID(LoopSimplifyID);