2013-08-13 05:02:02 +08:00
|
|
|
//===-- SelectionDAGBuilder.h - Selection-DAG building --------*- C++ -*---===//
|
2008-09-04 00:12:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements routines for translating from LLVM IR into SelectionDAG IR.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-24 02:04:58 +08:00
|
|
|
#ifndef SELECTIONDAGBUILDER_H
|
|
|
|
#define SELECTIONDAGBUILDER_H
|
2008-09-04 00:12:24 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/APInt.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
2008-09-04 00:12:24 +08:00
|
|
|
#include "llvm/CodeGen/SelectionDAGNodes.h"
|
2009-02-03 09:32:22 +08:00
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2008-09-04 00:12:24 +08:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-09-04 00:12:24 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class AliasAnalysis;
|
|
|
|
class AllocaInst;
|
|
|
|
class BasicBlock;
|
|
|
|
class BitCastInst;
|
|
|
|
class BranchInst;
|
|
|
|
class CallInst;
|
2010-08-27 07:35:15 +08:00
|
|
|
class DbgValueInst;
|
2008-09-04 00:12:24 +08:00
|
|
|
class ExtractElementInst;
|
|
|
|
class ExtractValueInst;
|
|
|
|
class FCmpInst;
|
|
|
|
class FPExtInst;
|
|
|
|
class FPToSIInst;
|
|
|
|
class FPToUIInst;
|
|
|
|
class FPTruncInst;
|
|
|
|
class Function;
|
2009-11-24 01:16:22 +08:00
|
|
|
class FunctionLoweringInfo;
|
2008-09-04 00:12:24 +08:00
|
|
|
class GetElementPtrInst;
|
|
|
|
class GCFunctionInfo;
|
|
|
|
class ICmpInst;
|
|
|
|
class IntToPtrInst;
|
2009-10-28 08:19:10 +08:00
|
|
|
class IndirectBrInst;
|
2008-09-04 00:12:24 +08:00
|
|
|
class InvokeInst;
|
|
|
|
class InsertElementInst;
|
|
|
|
class InsertValueInst;
|
|
|
|
class Instruction;
|
|
|
|
class LoadInst;
|
|
|
|
class MachineBasicBlock;
|
|
|
|
class MachineInstr;
|
|
|
|
class MachineRegisterInfo;
|
2010-04-29 07:08:54 +08:00
|
|
|
class MDNode;
|
2008-09-04 00:12:24 +08:00
|
|
|
class PHINode;
|
|
|
|
class PtrToIntInst;
|
|
|
|
class ReturnInst;
|
2010-07-16 08:02:08 +08:00
|
|
|
class SDDbgValue;
|
2008-09-04 00:12:24 +08:00
|
|
|
class SExtInst;
|
|
|
|
class SelectInst;
|
|
|
|
class ShuffleVectorInst;
|
|
|
|
class SIToFPInst;
|
|
|
|
class StoreInst;
|
|
|
|
class SwitchInst;
|
2012-10-09 00:38:25 +08:00
|
|
|
class DataLayout;
|
2011-12-09 06:15:21 +08:00
|
|
|
class TargetLibraryInfo;
|
2008-09-04 00:12:24 +08:00
|
|
|
class TargetLowering;
|
|
|
|
class TruncInst;
|
|
|
|
class UIToFPInst;
|
|
|
|
class UnreachableInst;
|
|
|
|
class VAArgInst;
|
|
|
|
class ZExtInst;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-11-24 02:04:58 +08:00
|
|
|
/// SelectionDAGBuilder - This is the common target-independent lowering
|
2008-09-04 00:12:24 +08:00
|
|
|
/// implementation that is parameterized by a TargetLowering object.
|
|
|
|
///
|
2013-09-12 02:05:11 +08:00
|
|
|
class SelectionDAGBuilder {
|
2013-05-25 10:20:36 +08:00
|
|
|
/// CurInst - The current instruction being visited
|
|
|
|
const Instruction *CurInst;
|
2009-01-31 10:22:37 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
DenseMap<const Value*, SDValue> NodeMap;
|
2013-11-01 01:18:07 +08:00
|
|
|
|
2010-06-02 03:59:01 +08:00
|
|
|
/// UnusedArgNodeMap - Maps argument value for unused arguments. This is used
|
|
|
|
/// to preserve debug information for incoming arguments.
|
|
|
|
DenseMap<const Value*, SDValue> UnusedArgNodeMap;
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2010-07-16 08:02:08 +08:00
|
|
|
/// DanglingDebugInfo - Helper type for DanglingDebugInfoMap.
|
|
|
|
class DanglingDebugInfo {
|
2010-08-27 07:35:15 +08:00
|
|
|
const DbgValueInst* DI;
|
2010-07-16 08:02:08 +08:00
|
|
|
DebugLoc dl;
|
|
|
|
unsigned SDNodeOrder;
|
|
|
|
public:
|
|
|
|
DanglingDebugInfo() : DI(0), dl(DebugLoc()), SDNodeOrder(0) { }
|
2010-08-27 07:35:15 +08:00
|
|
|
DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) :
|
2010-07-16 08:02:08 +08:00
|
|
|
DI(di), dl(DL), SDNodeOrder(SDNO) { }
|
2010-08-27 07:35:15 +08:00
|
|
|
const DbgValueInst* getDI() { return DI; }
|
2010-07-16 08:02:08 +08:00
|
|
|
DebugLoc getdl() { return dl; }
|
|
|
|
unsigned getSDNodeOrder() { return SDNodeOrder; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not
|
|
|
|
/// yet seen the referent. We defer handling these until we do see it.
|
|
|
|
DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap;
|
|
|
|
|
2009-12-24 08:37:38 +08:00
|
|
|
public:
|
2008-09-04 00:12:24 +08:00
|
|
|
/// PendingLoads - Loads are not emitted to the program immediately. We bunch
|
|
|
|
/// them up and then emit token factor nodes when possible. This allows us to
|
|
|
|
/// get simple disambiguation between loads without worrying about alias
|
|
|
|
/// analysis.
|
|
|
|
SmallVector<SDValue, 8> PendingLoads;
|
2009-12-24 08:37:38 +08:00
|
|
|
private:
|
2008-09-04 00:12:24 +08:00
|
|
|
|
|
|
|
/// PendingExports - CopyToReg nodes that copy values to virtual registers
|
|
|
|
/// for export to other blocks need to be emitted before any terminator
|
|
|
|
/// instruction, but they have no other ordering requirements. We bunch them
|
|
|
|
/// up and the emit a single tokenfactor for them just before terminator
|
|
|
|
/// instructions.
|
|
|
|
SmallVector<SDValue, 8> PendingExports;
|
|
|
|
|
2009-12-19 07:32:53 +08:00
|
|
|
/// SDNodeOrder - A unique monotonically increasing number used to order the
|
|
|
|
/// SDNodes we create.
|
|
|
|
unsigned SDNodeOrder;
|
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
/// Case - A struct to record the Value for a switch case, and the
|
|
|
|
/// case's target basic block.
|
|
|
|
struct Case {
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
llvm-svn: 149481
2012-02-01 15:49:51 +08:00
|
|
|
const Constant *Low;
|
|
|
|
const Constant *High;
|
2008-09-04 00:12:24 +08:00
|
|
|
MachineBasicBlock* BB;
|
2011-07-30 06:25:21 +08:00
|
|
|
uint32_t ExtraWeight;
|
|
|
|
|
|
|
|
Case() : Low(0), High(0), BB(0), ExtraWeight(0) { }
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
llvm-svn: 149481
2012-02-01 15:49:51 +08:00
|
|
|
Case(const Constant *low, const Constant *high, MachineBasicBlock *bb,
|
2011-07-30 06:25:21 +08:00
|
|
|
uint32_t extraweight) : Low(low), High(high), BB(bb),
|
|
|
|
ExtraWeight(extraweight) { }
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2009-11-07 15:50:34 +08:00
|
|
|
APInt size() const {
|
|
|
|
const APInt &rHigh = cast<ConstantInt>(High)->getValue();
|
|
|
|
const APInt &rLow = cast<ConstantInt>(Low)->getValue();
|
2008-09-04 00:12:24 +08:00
|
|
|
return (rHigh - rLow + 1ULL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CaseBits {
|
|
|
|
uint64_t Mask;
|
|
|
|
MachineBasicBlock* BB;
|
|
|
|
unsigned Bits;
|
2012-08-25 02:14:27 +08:00
|
|
|
uint32_t ExtraWeight;
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2012-08-25 02:14:27 +08:00
|
|
|
CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits,
|
|
|
|
uint32_t Weight):
|
|
|
|
Mask(mask), BB(bb), Bits(bits), ExtraWeight(Weight) { }
|
2008-09-04 00:12:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<Case> CaseVector;
|
|
|
|
typedef std::vector<CaseBits> CaseBitsVector;
|
|
|
|
typedef CaseVector::iterator CaseItr;
|
|
|
|
typedef std::pair<CaseItr, CaseItr> CaseRange;
|
|
|
|
|
|
|
|
/// CaseRec - A struct with ctor used in lowering switches to a binary tree
|
|
|
|
/// of conditional branches.
|
|
|
|
struct CaseRec {
|
2010-04-15 09:51:59 +08:00
|
|
|
CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge,
|
|
|
|
CaseRange r) :
|
2008-09-04 00:12:24 +08:00
|
|
|
CaseBB(bb), LT(lt), GE(ge), Range(r) {}
|
|
|
|
|
|
|
|
/// CaseBB - The MBB in which to emit the compare and branch
|
|
|
|
MachineBasicBlock *CaseBB;
|
|
|
|
/// LT, GE - If nonzero, we know the current case value must be less-than or
|
|
|
|
/// greater-than-or-equal-to these Constants.
|
2010-04-15 09:51:59 +08:00
|
|
|
const Constant *LT;
|
|
|
|
const Constant *GE;
|
2008-09-04 00:12:24 +08:00
|
|
|
/// Range - A pair of iterators representing the range of case values to be
|
|
|
|
/// processed at this point in the binary search tree.
|
|
|
|
CaseRange Range;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<CaseRec> CaseRecVector;
|
|
|
|
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
llvm-svn: 190328
2013-09-10 03:14:35 +08:00
|
|
|
/// The comparison function for sorting the switch case values in the vector.
|
|
|
|
/// WARNING: Case ranges should be disjoint!
|
|
|
|
struct CaseCmp {
|
|
|
|
bool operator()(const Case &C1, const Case &C2) {
|
|
|
|
assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High));
|
|
|
|
const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
|
|
|
|
const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
|
|
|
|
return CI1->getValue().slt(CI2->getValue());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
struct CaseBitsCmp {
|
2010-01-02 07:37:34 +08:00
|
|
|
bool operator()(const CaseBits &C1, const CaseBits &C2) {
|
2008-09-04 00:12:24 +08:00
|
|
|
return C1.Bits > C2.Bits;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-01-02 07:37:34 +08:00
|
|
|
size_t Clusterify(CaseVector &Cases, const SwitchInst &SI);
|
2008-12-24 06:25:27 +08:00
|
|
|
|
2009-11-24 02:04:58 +08:00
|
|
|
/// CaseBlock - This structure is used to communicate between
|
|
|
|
/// SelectionDAGBuilder and SDISel for the code generation of additional basic
|
|
|
|
/// blocks needed by multi-case switch statements.
|
2008-09-04 00:12:24 +08:00
|
|
|
struct CaseBlock {
|
2010-04-15 09:51:59 +08:00
|
|
|
CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs,
|
|
|
|
const Value *cmpmiddle,
|
2008-09-04 00:12:24 +08:00
|
|
|
MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
|
2011-07-30 06:25:21 +08:00
|
|
|
MachineBasicBlock *me,
|
|
|
|
uint32_t trueweight = 0, uint32_t falseweight = 0)
|
2008-09-04 00:12:24 +08:00
|
|
|
: CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
|
2011-07-30 06:25:21 +08:00
|
|
|
TrueBB(truebb), FalseBB(falsebb), ThisBB(me),
|
|
|
|
TrueWeight(trueweight), FalseWeight(falseweight) { }
|
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
// CC - the condition code to use for the case block's setcc node
|
|
|
|
ISD::CondCode CC;
|
2011-07-30 06:25:21 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
// CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
|
|
|
|
// Emit by default LHS op RHS. MHS is used for range comparisons:
|
|
|
|
// If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value *CmpLHS, *CmpMHS, *CmpRHS;
|
2011-07-30 06:25:21 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
// TrueBB/FalseBB - the block to branch to if the setcc is true/false.
|
|
|
|
MachineBasicBlock *TrueBB, *FalseBB;
|
2011-07-30 06:25:21 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
// ThisBB - the block into which to emit the code for the setcc and branches
|
|
|
|
MachineBasicBlock *ThisBB;
|
2011-07-30 06:25:21 +08:00
|
|
|
|
|
|
|
// TrueWeight/FalseWeight - branch weights.
|
|
|
|
uint32_t TrueWeight, FalseWeight;
|
2008-09-04 00:12:24 +08:00
|
|
|
};
|
2011-07-30 06:25:21 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
struct JumpTable {
|
|
|
|
JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
|
|
|
|
MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
|
2013-11-01 01:18:07 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
/// Reg - the virtual register containing the index of the jump table entry
|
|
|
|
//. to jump to.
|
|
|
|
unsigned Reg;
|
|
|
|
/// JTI - the JumpTableIndex for this jump table in the function.
|
|
|
|
unsigned JTI;
|
|
|
|
/// MBB - the MBB into which to emit the code for the indirect jump.
|
|
|
|
MachineBasicBlock *MBB;
|
|
|
|
/// Default - the MBB of the default bb, which is a successor of the range
|
|
|
|
/// check MBB. This is when updating PHI nodes in successors.
|
|
|
|
MachineBasicBlock *Default;
|
|
|
|
};
|
|
|
|
struct JumpTableHeader {
|
2010-04-15 09:51:59 +08:00
|
|
|
JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H,
|
2008-09-04 00:12:24 +08:00
|
|
|
bool E = false):
|
|
|
|
First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {}
|
2008-12-24 06:25:27 +08:00
|
|
|
APInt First;
|
|
|
|
APInt Last;
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value *SValue;
|
2008-09-04 00:12:24 +08:00
|
|
|
MachineBasicBlock *HeaderBB;
|
|
|
|
bool Emitted;
|
|
|
|
};
|
|
|
|
typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
|
|
|
|
|
|
|
|
struct BitTestCase {
|
2012-08-25 02:14:27 +08:00
|
|
|
BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr,
|
|
|
|
uint32_t Weight):
|
|
|
|
Mask(M), ThisBB(T), TargetBB(Tr), ExtraWeight(Weight) { }
|
2008-09-04 00:12:24 +08:00
|
|
|
uint64_t Mask;
|
2010-01-02 07:37:34 +08:00
|
|
|
MachineBasicBlock *ThisBB;
|
|
|
|
MachineBasicBlock *TargetBB;
|
2012-08-25 02:14:27 +08:00
|
|
|
uint32_t ExtraWeight;
|
2008-09-04 00:12:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef SmallVector<BitTestCase, 3> BitTestInfo;
|
|
|
|
|
|
|
|
struct BitTestBlock {
|
2010-04-15 09:51:59 +08:00
|
|
|
BitTestBlock(APInt F, APInt R, const Value* SV,
|
2012-12-19 20:23:01 +08:00
|
|
|
unsigned Rg, MVT RgVT, bool E,
|
2008-09-04 00:12:24 +08:00
|
|
|
MachineBasicBlock* P, MachineBasicBlock* D,
|
|
|
|
const BitTestInfo& C):
|
2011-01-06 09:02:44 +08:00
|
|
|
First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E),
|
2008-09-04 00:12:24 +08:00
|
|
|
Parent(P), Default(D), Cases(C) { }
|
2008-12-24 06:25:27 +08:00
|
|
|
APInt First;
|
|
|
|
APInt Range;
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value *SValue;
|
2008-09-04 00:12:24 +08:00
|
|
|
unsigned Reg;
|
2012-12-19 20:23:01 +08:00
|
|
|
MVT RegVT;
|
2008-09-04 00:12:24 +08:00
|
|
|
bool Emitted;
|
|
|
|
MachineBasicBlock *Parent;
|
|
|
|
MachineBasicBlock *Default;
|
|
|
|
BitTestInfo Cases;
|
|
|
|
};
|
|
|
|
|
Teach selectiondag how to handle the stackprotectorcheck intrinsic.
Previously, generation of stack protectors was done exclusively in the
pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
splitting basic blocks at the IR level to create the success/failure basic
blocks in the tail of the basic block in question. As a result of this,
calls that would have qualified for the sibling call optimization were no
longer eligible for optimization since said calls were no longer right in
the "tail position" (i.e. the immediate predecessor of a ReturnInst
instruction).
Then it was noticed that since the sibling call optimization causes the
callee to reuse the caller's stack, if we could delay the generation of
the stack protector check until later in CodeGen after the sibling call
decision was made, we get both the tail call optimization and the stack
protector check!
A few goals in solving this problem were:
1. Preserve the architecture independence of stack protector generation.
2. Preserve the normal IR level stack protector check for platforms like
OpenBSD for which we support platform specific stack protector
generation.
The main problem that guided the present solution is that one can not
solve this problem in an architecture independent manner at the IR level
only. This is because:
1. The decision on whether or not to perform a sibling call on certain
platforms (for instance i386) requires lower level information
related to available registers that can not be known at the IR level.
2. Even if the previous point were not true, the decision on whether to
perform a tail call is done in LowerCallTo in SelectionDAG which
occurs after the Stack Protector Pass. As a result, one would need to
put the relevant callinst into the stack protector check success
basic block (where the return inst is placed) and then move it back
later at SelectionDAG/MI time before the stack protector check if the
tail call optimization failed. The MI level option was nixed
immediately since it would require platform specific pattern
matching. The SelectionDAG level option was nixed because
SelectionDAG only processes one IR level basic block at a time
implying one could not create a DAG Combine to move the callinst.
To get around this problem a few things were realized:
1. While one can not handle multiple IR level basic blocks at the
SelectionDAG Level, one can generate multiple machine basic blocks
for one IR level basic block. This is how we handle bit tests and
switches.
2. At the MI level, tail calls are represented via a special return
MIInst called "tcreturn". Thus if we know the basic block in which we
wish to insert the stack protector check, we get the correct behavior
by always inserting the stack protector check right before the return
statement. This is a "magical transformation" since no matter where
the stack protector check intrinsic is, we always insert the stack
protector check code at the end of the BB.
Given the aforementioned constraints, the following solution was devised:
1. On platforms that do not support SelectionDAG stack protector check
generation, allow for the normal IR level stack protector check
generation to continue.
2. On platforms that do support SelectionDAG stack protector check
generation:
a. Use the IR level stack protector pass to decide if a stack
protector is required/which BB we insert the stack protector check
in by reusing the logic already therein. If we wish to generate a
stack protector check in a basic block, we place a special IR
intrinsic called llvm.stackprotectorcheck right before the BB's
returninst or if there is a callinst that could potentially be
sibling call optimized, before the call inst.
b. Then when a BB with said intrinsic is processed, we codegen the BB
normally via SelectBasicBlock. In said process, when we visit the
stack protector check, we do not actually emit anything into the
BB. Instead, we just initialize the stack protector descriptor
class (which involves stashing information/creating the success
mbbb and the failure mbb if we have not created one for this
function yet) and export the guard variable that we are going to
compare.
c. After we finish selecting the basic block, in FinishBasicBlock if
the StackProtectorDescriptor attached to the SelectionDAGBuilder is
initialized, we first find a splice point in the parent basic block
before the terminator and then splice the terminator of said basic
block into the success basic block. Then we code-gen a new tail for
the parent basic block consisting of the two loads, the comparison,
and finally two branches to the success/failure basic blocks. We
conclude by code-gening the failure basic block if we have not
code-gened it already (all stack protector checks we generate in
the same function, use the same failure basic block).
llvm-svn: 188755
2013-08-20 15:00:16 +08:00
|
|
|
/// A class which encapsulates all of the information needed to generate a
|
|
|
|
/// stack protector check and signals to isel via its state being initialized
|
|
|
|
/// that a stack protector needs to be generated.
|
|
|
|
///
|
|
|
|
/// *NOTE* The following is a high level documentation of SelectionDAG Stack
|
|
|
|
/// Protector Generation. The reason that it is placed here is for a lack of
|
|
|
|
/// other good places to stick it.
|
|
|
|
///
|
|
|
|
/// High Level Overview of SelectionDAG Stack Protector Generation:
|
|
|
|
///
|
|
|
|
/// Previously, generation of stack protectors was done exclusively in the
|
|
|
|
/// pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
|
|
|
|
/// splitting basic blocks at the IR level to create the success/failure basic
|
|
|
|
/// blocks in the tail of the basic block in question. As a result of this,
|
|
|
|
/// calls that would have qualified for the sibling call optimization were no
|
|
|
|
/// longer eligible for optimization since said calls were no longer right in
|
|
|
|
/// the "tail position" (i.e. the immediate predecessor of a ReturnInst
|
|
|
|
/// instruction).
|
|
|
|
///
|
|
|
|
/// Then it was noticed that since the sibling call optimization causes the
|
|
|
|
/// callee to reuse the caller's stack, if we could delay the generation of
|
|
|
|
/// the stack protector check until later in CodeGen after the sibling call
|
|
|
|
/// decision was made, we get both the tail call optimization and the stack
|
|
|
|
/// protector check!
|
|
|
|
///
|
|
|
|
/// A few goals in solving this problem were:
|
|
|
|
///
|
|
|
|
/// 1. Preserve the architecture independence of stack protector generation.
|
|
|
|
///
|
|
|
|
/// 2. Preserve the normal IR level stack protector check for platforms like
|
|
|
|
/// OpenBSD for which we support platform specific stack protector
|
|
|
|
/// generation.
|
|
|
|
///
|
|
|
|
/// The main problem that guided the present solution is that one can not
|
|
|
|
/// solve this problem in an architecture independent manner at the IR level
|
|
|
|
/// only. This is because:
|
|
|
|
///
|
|
|
|
/// 1. The decision on whether or not to perform a sibling call on certain
|
|
|
|
/// platforms (for instance i386) requires lower level information
|
|
|
|
/// related to available registers that can not be known at the IR level.
|
|
|
|
///
|
|
|
|
/// 2. Even if the previous point were not true, the decision on whether to
|
|
|
|
/// perform a tail call is done in LowerCallTo in SelectionDAG which
|
|
|
|
/// occurs after the Stack Protector Pass. As a result, one would need to
|
|
|
|
/// put the relevant callinst into the stack protector check success
|
|
|
|
/// basic block (where the return inst is placed) and then move it back
|
|
|
|
/// later at SelectionDAG/MI time before the stack protector check if the
|
|
|
|
/// tail call optimization failed. The MI level option was nixed
|
|
|
|
/// immediately since it would require platform specific pattern
|
|
|
|
/// matching. The SelectionDAG level option was nixed because
|
|
|
|
/// SelectionDAG only processes one IR level basic block at a time
|
|
|
|
/// implying one could not create a DAG Combine to move the callinst.
|
|
|
|
///
|
|
|
|
/// To get around this problem a few things were realized:
|
|
|
|
///
|
|
|
|
/// 1. While one can not handle multiple IR level basic blocks at the
|
|
|
|
/// SelectionDAG Level, one can generate multiple machine basic blocks
|
|
|
|
/// for one IR level basic block. This is how we handle bit tests and
|
|
|
|
/// switches.
|
|
|
|
///
|
|
|
|
/// 2. At the MI level, tail calls are represented via a special return
|
|
|
|
/// MIInst called "tcreturn". Thus if we know the basic block in which we
|
|
|
|
/// wish to insert the stack protector check, we get the correct behavior
|
|
|
|
/// by always inserting the stack protector check right before the return
|
|
|
|
/// statement. This is a "magical transformation" since no matter where
|
|
|
|
/// the stack protector check intrinsic is, we always insert the stack
|
|
|
|
/// protector check code at the end of the BB.
|
|
|
|
///
|
|
|
|
/// Given the aforementioned constraints, the following solution was devised:
|
|
|
|
///
|
|
|
|
/// 1. On platforms that do not support SelectionDAG stack protector check
|
|
|
|
/// generation, allow for the normal IR level stack protector check
|
|
|
|
/// generation to continue.
|
|
|
|
///
|
|
|
|
/// 2. On platforms that do support SelectionDAG stack protector check
|
|
|
|
/// generation:
|
|
|
|
///
|
|
|
|
/// a. Use the IR level stack protector pass to decide if a stack
|
|
|
|
/// protector is required/which BB we insert the stack protector check
|
|
|
|
/// in by reusing the logic already therein. If we wish to generate a
|
|
|
|
/// stack protector check in a basic block, we place a special IR
|
|
|
|
/// intrinsic called llvm.stackprotectorcheck right before the BB's
|
|
|
|
/// returninst or if there is a callinst that could potentially be
|
|
|
|
/// sibling call optimized, before the call inst.
|
|
|
|
///
|
|
|
|
/// b. Then when a BB with said intrinsic is processed, we codegen the BB
|
|
|
|
/// normally via SelectBasicBlock. In said process, when we visit the
|
|
|
|
/// stack protector check, we do not actually emit anything into the
|
|
|
|
/// BB. Instead, we just initialize the stack protector descriptor
|
|
|
|
/// class (which involves stashing information/creating the success
|
|
|
|
/// mbbb and the failure mbb if we have not created one for this
|
|
|
|
/// function yet) and export the guard variable that we are going to
|
|
|
|
/// compare.
|
|
|
|
///
|
|
|
|
/// c. After we finish selecting the basic block, in FinishBasicBlock if
|
|
|
|
/// the StackProtectorDescriptor attached to the SelectionDAGBuilder is
|
|
|
|
/// initialized, we first find a splice point in the parent basic block
|
|
|
|
/// before the terminator and then splice the terminator of said basic
|
|
|
|
/// block into the success basic block. Then we code-gen a new tail for
|
|
|
|
/// the parent basic block consisting of the two loads, the comparison,
|
|
|
|
/// and finally two branches to the success/failure basic blocks. We
|
|
|
|
/// conclude by code-gening the failure basic block if we have not
|
|
|
|
/// code-gened it already (all stack protector checks we generate in
|
|
|
|
/// the same function, use the same failure basic block).
|
|
|
|
class StackProtectorDescriptor {
|
|
|
|
public:
|
|
|
|
StackProtectorDescriptor() : ParentMBB(0), SuccessMBB(0), FailureMBB(0),
|
|
|
|
Guard(0) { }
|
|
|
|
~StackProtectorDescriptor() { }
|
|
|
|
|
|
|
|
/// Returns true if all fields of the stack protector descriptor are
|
|
|
|
/// initialized implying that we should/are ready to emit a stack protector.
|
|
|
|
bool shouldEmitStackProtector() const {
|
|
|
|
return ParentMBB && SuccessMBB && FailureMBB && Guard;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize the stack protector descriptor structure for a new basic
|
|
|
|
/// block.
|
|
|
|
void initialize(const BasicBlock *BB,
|
|
|
|
MachineBasicBlock *MBB,
|
|
|
|
const CallInst &StackProtCheckCall) {
|
|
|
|
// Make sure we are not initialized yet.
|
|
|
|
assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is "
|
|
|
|
"already initialized!");
|
|
|
|
ParentMBB = MBB;
|
|
|
|
SuccessMBB = AddSuccessorMBB(BB, MBB);
|
|
|
|
FailureMBB = AddSuccessorMBB(BB, MBB, FailureMBB);
|
|
|
|
if (!Guard)
|
|
|
|
Guard = StackProtCheckCall.getArgOperand(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reset state that changes when we handle different basic blocks.
|
|
|
|
///
|
|
|
|
/// This currently includes:
|
|
|
|
///
|
|
|
|
/// 1. The specific basic block we are generating a
|
|
|
|
/// stack protector for (ParentMBB).
|
|
|
|
///
|
|
|
|
/// 2. The successor machine basic block that will contain the tail of
|
|
|
|
/// parent mbb after we create the stack protector check (SuccessMBB). This
|
|
|
|
/// BB is visited only on stack protector check success.
|
|
|
|
void resetPerBBState() {
|
|
|
|
ParentMBB = 0;
|
|
|
|
SuccessMBB = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reset state that only changes when we switch functions.
|
|
|
|
///
|
|
|
|
/// This currently includes:
|
|
|
|
///
|
|
|
|
/// 1. FailureMBB since we reuse the failure code path for all stack
|
|
|
|
/// protector checks created in an individual function.
|
|
|
|
///
|
|
|
|
/// 2.The guard variable since the guard variable we are checking against is
|
|
|
|
/// always the same.
|
|
|
|
void resetPerFunctionState() {
|
|
|
|
FailureMBB = 0;
|
|
|
|
Guard = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *getParentMBB() { return ParentMBB; }
|
|
|
|
MachineBasicBlock *getSuccessMBB() { return SuccessMBB; }
|
|
|
|
MachineBasicBlock *getFailureMBB() { return FailureMBB; }
|
|
|
|
const Value *getGuard() { return Guard; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// The basic block for which we are generating the stack protector.
|
|
|
|
///
|
|
|
|
/// As a result of stack protector generation, we will splice the
|
|
|
|
/// terminators of this basic block into the successor mbb SuccessMBB and
|
|
|
|
/// replace it with a compare/branch to the successor mbbs
|
|
|
|
/// SuccessMBB/FailureMBB depending on whether or not the stack protector
|
|
|
|
/// was violated.
|
|
|
|
MachineBasicBlock *ParentMBB;
|
|
|
|
|
|
|
|
/// A basic block visited on stack protector check success that contains the
|
|
|
|
/// terminators of ParentMBB.
|
|
|
|
MachineBasicBlock *SuccessMBB;
|
|
|
|
|
|
|
|
/// This basic block visited on stack protector check failure that will
|
|
|
|
/// contain a call to __stack_chk_fail().
|
|
|
|
MachineBasicBlock *FailureMBB;
|
|
|
|
|
|
|
|
/// The guard variable which we will compare against the stored value in the
|
|
|
|
/// stack protector stack slot.
|
|
|
|
const Value *Guard;
|
|
|
|
|
|
|
|
/// Add a successor machine basic block to ParentMBB. If the successor mbb
|
|
|
|
/// has not been created yet (i.e. if SuccMBB = 0), then the machine basic
|
|
|
|
/// block will be created.
|
|
|
|
MachineBasicBlock *AddSuccessorMBB(const BasicBlock *BB,
|
|
|
|
MachineBasicBlock *ParentMBB,
|
|
|
|
MachineBasicBlock *SuccMBB = 0);
|
|
|
|
};
|
|
|
|
|
2013-06-20 05:36:55 +08:00
|
|
|
private:
|
2010-04-20 03:05:59 +08:00
|
|
|
const TargetMachine &TM;
|
2013-06-20 05:36:55 +08:00
|
|
|
public:
|
2008-09-04 00:12:24 +08:00
|
|
|
SelectionDAG &DAG;
|
2012-10-09 00:38:25 +08:00
|
|
|
const DataLayout *TD;
|
2008-09-04 00:12:24 +08:00
|
|
|
AliasAnalysis *AA;
|
2011-12-09 06:15:21 +08:00
|
|
|
const TargetLibraryInfo *LibInfo;
|
2008-09-04 00:12:24 +08:00
|
|
|
|
|
|
|
/// SwitchCases - Vector of CaseBlock structures used to communicate
|
|
|
|
/// SwitchInst code generation information.
|
|
|
|
std::vector<CaseBlock> SwitchCases;
|
|
|
|
/// JTCases - Vector of JumpTable structures used to communicate
|
|
|
|
/// SwitchInst code generation information.
|
|
|
|
std::vector<JumpTableBlock> JTCases;
|
|
|
|
/// BitTestCases - Vector of BitTestBlock structures used to communicate
|
|
|
|
/// SwitchInst code generation information.
|
|
|
|
std::vector<BitTestBlock> BitTestCases;
|
Teach selectiondag how to handle the stackprotectorcheck intrinsic.
Previously, generation of stack protectors was done exclusively in the
pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
splitting basic blocks at the IR level to create the success/failure basic
blocks in the tail of the basic block in question. As a result of this,
calls that would have qualified for the sibling call optimization were no
longer eligible for optimization since said calls were no longer right in
the "tail position" (i.e. the immediate predecessor of a ReturnInst
instruction).
Then it was noticed that since the sibling call optimization causes the
callee to reuse the caller's stack, if we could delay the generation of
the stack protector check until later in CodeGen after the sibling call
decision was made, we get both the tail call optimization and the stack
protector check!
A few goals in solving this problem were:
1. Preserve the architecture independence of stack protector generation.
2. Preserve the normal IR level stack protector check for platforms like
OpenBSD for which we support platform specific stack protector
generation.
The main problem that guided the present solution is that one can not
solve this problem in an architecture independent manner at the IR level
only. This is because:
1. The decision on whether or not to perform a sibling call on certain
platforms (for instance i386) requires lower level information
related to available registers that can not be known at the IR level.
2. Even if the previous point were not true, the decision on whether to
perform a tail call is done in LowerCallTo in SelectionDAG which
occurs after the Stack Protector Pass. As a result, one would need to
put the relevant callinst into the stack protector check success
basic block (where the return inst is placed) and then move it back
later at SelectionDAG/MI time before the stack protector check if the
tail call optimization failed. The MI level option was nixed
immediately since it would require platform specific pattern
matching. The SelectionDAG level option was nixed because
SelectionDAG only processes one IR level basic block at a time
implying one could not create a DAG Combine to move the callinst.
To get around this problem a few things were realized:
1. While one can not handle multiple IR level basic blocks at the
SelectionDAG Level, one can generate multiple machine basic blocks
for one IR level basic block. This is how we handle bit tests and
switches.
2. At the MI level, tail calls are represented via a special return
MIInst called "tcreturn". Thus if we know the basic block in which we
wish to insert the stack protector check, we get the correct behavior
by always inserting the stack protector check right before the return
statement. This is a "magical transformation" since no matter where
the stack protector check intrinsic is, we always insert the stack
protector check code at the end of the BB.
Given the aforementioned constraints, the following solution was devised:
1. On platforms that do not support SelectionDAG stack protector check
generation, allow for the normal IR level stack protector check
generation to continue.
2. On platforms that do support SelectionDAG stack protector check
generation:
a. Use the IR level stack protector pass to decide if a stack
protector is required/which BB we insert the stack protector check
in by reusing the logic already therein. If we wish to generate a
stack protector check in a basic block, we place a special IR
intrinsic called llvm.stackprotectorcheck right before the BB's
returninst or if there is a callinst that could potentially be
sibling call optimized, before the call inst.
b. Then when a BB with said intrinsic is processed, we codegen the BB
normally via SelectBasicBlock. In said process, when we visit the
stack protector check, we do not actually emit anything into the
BB. Instead, we just initialize the stack protector descriptor
class (which involves stashing information/creating the success
mbbb and the failure mbb if we have not created one for this
function yet) and export the guard variable that we are going to
compare.
c. After we finish selecting the basic block, in FinishBasicBlock if
the StackProtectorDescriptor attached to the SelectionDAGBuilder is
initialized, we first find a splice point in the parent basic block
before the terminator and then splice the terminator of said basic
block into the success basic block. Then we code-gen a new tail for
the parent basic block consisting of the two loads, the comparison,
and finally two branches to the success/failure basic blocks. We
conclude by code-gening the failure basic block if we have not
code-gened it already (all stack protector checks we generate in
the same function, use the same failure basic block).
llvm-svn: 188755
2013-08-20 15:00:16 +08:00
|
|
|
/// A StackProtectorDescriptor structure used to communicate stack protector
|
|
|
|
/// information in between SelectBasicBlock and FinishBasicBlock.
|
|
|
|
StackProtectorDescriptor SPDescriptor;
|
2009-09-19 05:02:19 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
// Emit PHI-node-operand constants only once even if used by multiple
|
|
|
|
// PHI nodes.
|
2010-04-15 09:51:59 +08:00
|
|
|
DenseMap<const Constant *, unsigned> ConstantsOut;
|
2008-09-04 00:12:24 +08:00
|
|
|
|
|
|
|
/// FuncInfo - Information about the function as a whole.
|
|
|
|
///
|
|
|
|
FunctionLoweringInfo &FuncInfo;
|
2009-02-20 05:12:54 +08:00
|
|
|
|
2009-04-29 08:15:41 +08:00
|
|
|
/// OptLevel - What optimization level we're generating code for.
|
2013-11-01 01:18:07 +08:00
|
|
|
///
|
2009-04-30 07:29:43 +08:00
|
|
|
CodeGenOpt::Level OptLevel;
|
2013-11-01 01:18:07 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
/// GFI - Garbage collection metadata for the function.
|
|
|
|
GCFunctionInfo *GFI;
|
|
|
|
|
2011-10-06 06:24:35 +08:00
|
|
|
/// LPadToCallSiteMap - Map a landing pad to the call site indexes.
|
|
|
|
DenseMap<MachineBasicBlock*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
|
2011-10-05 06:00:35 +08:00
|
|
|
|
Major calling convention code refactoring.
Instead of awkwardly encoding calling-convention information with ISD::CALL,
ISD::FORMAL_ARGUMENTS, ISD::RET, and ISD::ARG_FLAGS nodes, TargetLowering
provides three virtual functions for targets to override:
LowerFormalArguments, LowerCall, and LowerRet, which replace the custom
lowering done on the special nodes. They provide the same information, but
in a more immediately usable format.
This also reworks much of the target-independent tail call logic. The
decision of whether or not to perform a tail call is now cleanly split
between target-independent portions, and the target dependent portion
in IsEligibleForTailCallOptimization.
This also synchronizes all in-tree targets, to help enable future
refactoring and feature work.
llvm-svn: 78142
2009-08-05 09:29:28 +08:00
|
|
|
/// HasTailCall - This is set to true if a call in the current
|
|
|
|
/// block has been translated as a tail call. In this case,
|
|
|
|
/// no subsequent DAG nodes should be created.
|
|
|
|
///
|
|
|
|
bool HasTailCall;
|
|
|
|
|
2009-07-13 12:09:18 +08:00
|
|
|
LLVMContext *Context;
|
|
|
|
|
2010-04-20 03:05:59 +08:00
|
|
|
SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
|
2009-11-24 02:04:58 +08:00
|
|
|
CodeGenOpt::Level ol)
|
2013-05-25 10:20:36 +08:00
|
|
|
: CurInst(NULL), SDNodeOrder(0), TM(dag.getTarget()),
|
2010-04-20 03:05:59 +08:00
|
|
|
DAG(dag), FuncInfo(funcinfo), OptLevel(ol),
|
2012-08-22 08:42:39 +08:00
|
|
|
HasTailCall(false) {
|
2008-09-04 00:12:24 +08:00
|
|
|
}
|
|
|
|
|
2011-12-09 06:15:21 +08:00
|
|
|
void init(GCFunctionInfo *gfi, AliasAnalysis &aa,
|
|
|
|
const TargetLibraryInfo *li);
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2010-04-15 02:24:06 +08:00
|
|
|
/// clear - Clear out the current SelectionDAG and the associated
|
2009-11-24 02:04:58 +08:00
|
|
|
/// state and prepare this SelectionDAGBuilder object to be used
|
2008-09-04 00:12:24 +08:00
|
|
|
/// for a new block. This doesn't clear out information about
|
|
|
|
/// additional blocks that are needed to complete switch lowering
|
|
|
|
/// or PHI node updating; that information is cleared out as it is
|
|
|
|
/// consumed.
|
|
|
|
void clear();
|
|
|
|
|
2011-05-24 01:44:13 +08:00
|
|
|
/// clearDanglingDebugInfo - Clear the dangling debug information
|
2012-06-02 18:20:22 +08:00
|
|
|
/// map. This function is separated from the clear so that debug
|
2011-05-24 01:44:13 +08:00
|
|
|
/// information that is dangling in a basic block can be properly
|
|
|
|
/// resolved in a different basic block. This allows the
|
|
|
|
/// SelectionDAG to resolve dangling debug information attached
|
|
|
|
/// to PHI nodes.
|
|
|
|
void clearDanglingDebugInfo();
|
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
/// getRoot - Return the current virtual root of the Selection DAG,
|
|
|
|
/// flushing any PendingLoad items. This must be done before emitting
|
|
|
|
/// a store or any other node that may need to be ordered after any
|
|
|
|
/// prior load instructions.
|
|
|
|
///
|
|
|
|
SDValue getRoot();
|
|
|
|
|
|
|
|
/// getControlRoot - Similar to getRoot, but instead of flushing all the
|
|
|
|
/// PendingLoad items, flush all the PendingExports items. It is necessary
|
|
|
|
/// to do this before emitting a terminator instruction.
|
|
|
|
///
|
|
|
|
SDValue getControlRoot();
|
|
|
|
|
2013-05-25 10:20:36 +08:00
|
|
|
SDLoc getCurSDLoc() const {
|
|
|
|
return SDLoc(CurInst, SDNodeOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugLoc getCurDebugLoc() const {
|
|
|
|
return CurInst ? CurInst->getDebugLoc() : DebugLoc();
|
|
|
|
}
|
2011-02-22 07:21:26 +08:00
|
|
|
|
2009-12-22 10:10:19 +08:00
|
|
|
unsigned getSDNodeOrder() const { return SDNodeOrder; }
|
|
|
|
|
2010-04-15 09:51:59 +08:00
|
|
|
void CopyValueToVirtualRegister(const Value *V, unsigned Reg);
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2010-04-15 09:51:59 +08:00
|
|
|
void visit(const Instruction &I);
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2010-04-15 09:51:59 +08:00
|
|
|
void visit(unsigned Opcode, const User &I);
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2010-07-16 08:02:08 +08:00
|
|
|
// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
|
|
|
|
// generate the debug data structures now that we've seen its definition.
|
|
|
|
void resolveDanglingDebugInfo(const Value *V, SDValue Val);
|
2008-09-04 00:12:24 +08:00
|
|
|
SDValue getValue(const Value *V);
|
2010-07-01 09:59:43 +08:00
|
|
|
SDValue getNonRegisterValue(const Value *V);
|
|
|
|
SDValue getValueImpl(const Value *V);
|
2008-09-04 00:12:24 +08:00
|
|
|
|
|
|
|
void setValue(const Value *V, SDValue NewN) {
|
|
|
|
SDValue &N = NodeMap[V];
|
|
|
|
assert(N.getNode() == 0 && "Already set a value for this node!");
|
|
|
|
N = NewN;
|
|
|
|
}
|
2013-11-01 01:18:07 +08:00
|
|
|
|
2010-06-02 03:59:01 +08:00
|
|
|
void setUnusedArgValue(const Value *V, SDValue NewN) {
|
|
|
|
SDValue &N = UnusedArgNodeMap[V];
|
|
|
|
assert(N.getNode() == 0 && "Already set a value for this node!");
|
|
|
|
N = NewN;
|
|
|
|
}
|
2008-09-04 00:12:24 +08:00
|
|
|
|
2010-04-15 09:51:59 +08:00
|
|
|
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
|
2008-09-04 00:12:24 +08:00
|
|
|
MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
|
2010-04-20 06:41:47 +08:00
|
|
|
MachineBasicBlock *SwitchBB, unsigned Opc);
|
2010-04-15 09:51:59 +08:00
|
|
|
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
|
2008-10-18 05:16:08 +08:00
|
|
|
MachineBasicBlock *FBB,
|
2010-04-20 06:41:47 +08:00
|
|
|
MachineBasicBlock *CurBB,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2008-09-04 00:12:24 +08:00
|
|
|
bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases);
|
2010-04-15 09:51:59 +08:00
|
|
|
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
|
|
|
|
void CopyToExportRegsIfNeeded(const Value *V);
|
|
|
|
void ExportFromCurrentBlock(const Value *V);
|
|
|
|
void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall,
|
2008-09-04 00:12:24 +08:00
|
|
|
MachineBasicBlock *LandingPad = NULL);
|
|
|
|
|
2013-11-01 01:18:24 +08:00
|
|
|
std::pair<SDValue, SDValue> LowerCallOperands(const CallInst &CI,
|
|
|
|
unsigned ArgIdx,
|
|
|
|
unsigned NumArgs,
|
|
|
|
SDValue Callee);
|
|
|
|
|
2010-10-01 03:44:31 +08:00
|
|
|
/// UpdateSplitBlock - When an MBB was split during scheduling, update the
|
|
|
|
/// references that ned to refer to the last resulting block.
|
|
|
|
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
|
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
private:
|
|
|
|
// Terminator instructions.
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitRet(const ReturnInst &I);
|
|
|
|
void visitBr(const BranchInst &I);
|
|
|
|
void visitSwitch(const SwitchInst &I);
|
|
|
|
void visitIndirectBr(const IndirectBrInst &I);
|
2010-07-16 07:42:21 +08:00
|
|
|
void visitUnreachable(const UnreachableInst &I) { /* noop */ }
|
2008-09-04 00:12:24 +08:00
|
|
|
|
|
|
|
// Helpers for visitSwitch
|
|
|
|
bool handleSmallSwitchRange(CaseRec& CR,
|
|
|
|
CaseRecVector& WorkList,
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value* SV,
|
2010-04-20 06:41:47 +08:00
|
|
|
MachineBasicBlock* Default,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2008-09-04 00:12:24 +08:00
|
|
|
bool handleJTSwitchCase(CaseRec& CR,
|
|
|
|
CaseRecVector& WorkList,
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value* SV,
|
2010-04-20 06:41:47 +08:00
|
|
|
MachineBasicBlock* Default,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2008-09-04 00:12:24 +08:00
|
|
|
bool handleBTSplitSwitchCase(CaseRec& CR,
|
|
|
|
CaseRecVector& WorkList,
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value* SV,
|
2010-04-20 06:41:47 +08:00
|
|
|
MachineBasicBlock* Default,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2008-09-04 00:12:24 +08:00
|
|
|
bool handleBitTestsSwitchCase(CaseRec& CR,
|
|
|
|
CaseRecVector& WorkList,
|
2010-04-15 09:51:59 +08:00
|
|
|
const Value* SV,
|
2010-04-20 06:41:47 +08:00
|
|
|
MachineBasicBlock* Default,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2011-06-17 04:22:37 +08:00
|
|
|
|
2011-12-21 04:03:10 +08:00
|
|
|
uint32_t getEdgeWeight(const MachineBasicBlock *Src,
|
|
|
|
const MachineBasicBlock *Dst) const;
|
2011-07-30 06:25:21 +08:00
|
|
|
void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
|
|
|
|
uint32_t Weight = 0);
|
2008-09-04 00:12:24 +08:00
|
|
|
public:
|
2010-04-20 06:41:47 +08:00
|
|
|
void visitSwitchCase(CaseBlock &CB,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
Teach selectiondag how to handle the stackprotectorcheck intrinsic.
Previously, generation of stack protectors was done exclusively in the
pre-SelectionDAG Codegen LLVM IR Pass "Stack Protector". This necessitated
splitting basic blocks at the IR level to create the success/failure basic
blocks in the tail of the basic block in question. As a result of this,
calls that would have qualified for the sibling call optimization were no
longer eligible for optimization since said calls were no longer right in
the "tail position" (i.e. the immediate predecessor of a ReturnInst
instruction).
Then it was noticed that since the sibling call optimization causes the
callee to reuse the caller's stack, if we could delay the generation of
the stack protector check until later in CodeGen after the sibling call
decision was made, we get both the tail call optimization and the stack
protector check!
A few goals in solving this problem were:
1. Preserve the architecture independence of stack protector generation.
2. Preserve the normal IR level stack protector check for platforms like
OpenBSD for which we support platform specific stack protector
generation.
The main problem that guided the present solution is that one can not
solve this problem in an architecture independent manner at the IR level
only. This is because:
1. The decision on whether or not to perform a sibling call on certain
platforms (for instance i386) requires lower level information
related to available registers that can not be known at the IR level.
2. Even if the previous point were not true, the decision on whether to
perform a tail call is done in LowerCallTo in SelectionDAG which
occurs after the Stack Protector Pass. As a result, one would need to
put the relevant callinst into the stack protector check success
basic block (where the return inst is placed) and then move it back
later at SelectionDAG/MI time before the stack protector check if the
tail call optimization failed. The MI level option was nixed
immediately since it would require platform specific pattern
matching. The SelectionDAG level option was nixed because
SelectionDAG only processes one IR level basic block at a time
implying one could not create a DAG Combine to move the callinst.
To get around this problem a few things were realized:
1. While one can not handle multiple IR level basic blocks at the
SelectionDAG Level, one can generate multiple machine basic blocks
for one IR level basic block. This is how we handle bit tests and
switches.
2. At the MI level, tail calls are represented via a special return
MIInst called "tcreturn". Thus if we know the basic block in which we
wish to insert the stack protector check, we get the correct behavior
by always inserting the stack protector check right before the return
statement. This is a "magical transformation" since no matter where
the stack protector check intrinsic is, we always insert the stack
protector check code at the end of the BB.
Given the aforementioned constraints, the following solution was devised:
1. On platforms that do not support SelectionDAG stack protector check
generation, allow for the normal IR level stack protector check
generation to continue.
2. On platforms that do support SelectionDAG stack protector check
generation:
a. Use the IR level stack protector pass to decide if a stack
protector is required/which BB we insert the stack protector check
in by reusing the logic already therein. If we wish to generate a
stack protector check in a basic block, we place a special IR
intrinsic called llvm.stackprotectorcheck right before the BB's
returninst or if there is a callinst that could potentially be
sibling call optimized, before the call inst.
b. Then when a BB with said intrinsic is processed, we codegen the BB
normally via SelectBasicBlock. In said process, when we visit the
stack protector check, we do not actually emit anything into the
BB. Instead, we just initialize the stack protector descriptor
class (which involves stashing information/creating the success
mbbb and the failure mbb if we have not created one for this
function yet) and export the guard variable that we are going to
compare.
c. After we finish selecting the basic block, in FinishBasicBlock if
the StackProtectorDescriptor attached to the SelectionDAGBuilder is
initialized, we first find a splice point in the parent basic block
before the terminator and then splice the terminator of said basic
block into the success basic block. Then we code-gen a new tail for
the parent basic block consisting of the two loads, the comparison,
and finally two branches to the success/failure basic blocks. We
conclude by code-gening the failure basic block if we have not
code-gened it already (all stack protector checks we generate in
the same function, use the same failure basic block).
llvm-svn: 188755
2013-08-20 15:00:16 +08:00
|
|
|
void visitSPDescriptorParent(StackProtectorDescriptor &SPD,
|
|
|
|
MachineBasicBlock *ParentBB);
|
|
|
|
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD);
|
2010-04-20 06:41:47 +08:00
|
|
|
void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB);
|
2011-01-06 09:02:44 +08:00
|
|
|
void visitBitTestCase(BitTestBlock &BB,
|
|
|
|
MachineBasicBlock* NextMBB,
|
2012-08-25 02:14:27 +08:00
|
|
|
uint32_t BranchWeightToNext,
|
2008-09-04 00:12:24 +08:00
|
|
|
unsigned Reg,
|
2010-04-20 06:41:47 +08:00
|
|
|
BitTestCase &B,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2008-09-04 00:12:24 +08:00
|
|
|
void visitJumpTable(JumpTable &JT);
|
2010-04-20 06:41:47 +08:00
|
|
|
void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH,
|
|
|
|
MachineBasicBlock *SwitchBB);
|
2013-11-01 01:18:07 +08:00
|
|
|
|
2008-09-04 00:12:24 +08:00
|
|
|
private:
|
|
|
|
// These all get lowered before this pass.
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitInvoke(const InvokeInst &I);
|
2011-07-31 14:30:59 +08:00
|
|
|
void visitResume(const ResumeInst &I);
|
2010-04-15 09:51:59 +08:00
|
|
|
|
|
|
|
void visitBinary(const User &I, unsigned OpCode);
|
|
|
|
void visitShift(const User &I, unsigned Opcode);
|
|
|
|
void visitAdd(const User &I) { visitBinary(I, ISD::ADD); }
|
|
|
|
void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
|
|
|
|
void visitSub(const User &I) { visitBinary(I, ISD::SUB); }
|
|
|
|
void visitFSub(const User &I);
|
|
|
|
void visitMul(const User &I) { visitBinary(I, ISD::MUL); }
|
|
|
|
void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
|
|
|
|
void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
|
|
|
|
void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
|
|
|
|
void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
|
|
|
|
void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
|
Emit a more efficient magic number multiplication for exact sdivs.
We have to do this in DAGBuilder instead of DAGCombiner, because the exact bit is lost after building.
struct foo { char x[24]; };
long bar(struct foo *a, struct foo *b) { return a-b; }
is now compiled into
movl 4(%esp), %eax
subl 8(%esp), %eax
sarl $3, %eax
imull $-1431655765, %eax, %eax
instead of
movl 4(%esp), %eax
subl 8(%esp), %eax
movl $715827883, %ecx
imull %ecx
movl %edx, %eax
shrl $31, %eax
sarl $2, %edx
addl %eax, %edx
movl %edx, %eax
llvm-svn: 134695
2011-07-08 18:31:30 +08:00
|
|
|
void visitSDiv(const User &I);
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
|
|
|
|
void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
|
|
|
|
void visitOr (const User &I) { visitBinary(I, ISD::OR); }
|
|
|
|
void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
|
|
|
|
void visitShl (const User &I) { visitShift(I, ISD::SHL); }
|
|
|
|
void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
|
|
|
|
void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
|
|
|
|
void visitICmp(const User &I);
|
|
|
|
void visitFCmp(const User &I);
|
2008-09-04 00:12:24 +08:00
|
|
|
// Visit the conversion instructions
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitTrunc(const User &I);
|
|
|
|
void visitZExt(const User &I);
|
|
|
|
void visitSExt(const User &I);
|
|
|
|
void visitFPTrunc(const User &I);
|
|
|
|
void visitFPExt(const User &I);
|
|
|
|
void visitFPToUI(const User &I);
|
|
|
|
void visitFPToSI(const User &I);
|
|
|
|
void visitUIToFP(const User &I);
|
|
|
|
void visitSIToFP(const User &I);
|
|
|
|
void visitPtrToInt(const User &I);
|
|
|
|
void visitIntToPtr(const User &I);
|
|
|
|
void visitBitCast(const User &I);
|
|
|
|
|
|
|
|
void visitExtractElement(const User &I);
|
|
|
|
void visitInsertElement(const User &I);
|
|
|
|
void visitShuffleVector(const User &I);
|
|
|
|
|
|
|
|
void visitExtractValue(const ExtractValueInst &I);
|
|
|
|
void visitInsertValue(const InsertValueInst &I);
|
2011-08-13 04:24:12 +08:00
|
|
|
void visitLandingPad(const LandingPadInst &I);
|
2010-04-15 09:51:59 +08:00
|
|
|
|
|
|
|
void visitGetElementPtr(const User &I);
|
|
|
|
void visitSelect(const User &I);
|
|
|
|
|
|
|
|
void visitAlloca(const AllocaInst &I);
|
|
|
|
void visitLoad(const LoadInst &I);
|
|
|
|
void visitStore(const StoreInst &I);
|
2011-07-29 05:48:00 +08:00
|
|
|
void visitAtomicCmpXchg(const AtomicCmpXchgInst &I);
|
|
|
|
void visitAtomicRMW(const AtomicRMWInst &I);
|
2011-07-26 07:16:38 +08:00
|
|
|
void visitFence(const FenceInst &I);
|
2010-04-20 23:00:41 +08:00
|
|
|
void visitPHI(const PHINode &I);
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitCall(const CallInst &I);
|
|
|
|
bool visitMemCmpCall(const CallInst &I);
|
2013-08-20 17:38:48 +08:00
|
|
|
bool visitMemChrCall(const CallInst &I);
|
2013-08-16 19:29:37 +08:00
|
|
|
bool visitStrCpyCall(const CallInst &I, bool isStpcpy);
|
2013-08-16 19:21:54 +08:00
|
|
|
bool visitStrCmpCall(const CallInst &I);
|
2013-08-16 19:41:43 +08:00
|
|
|
bool visitStrLenCall(const CallInst &I);
|
|
|
|
bool visitStrNLenCall(const CallInst &I);
|
2012-08-04 07:29:17 +08:00
|
|
|
bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode);
|
2011-08-25 04:50:09 +08:00
|
|
|
void visitAtomicLoad(const LoadInst &I);
|
|
|
|
void visitAtomicStore(const StoreInst &I);
|
|
|
|
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitInlineAsm(ImmutableCallSite CS);
|
|
|
|
const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
|
|
|
|
void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
|
|
|
|
|
|
|
|
void visitVAStart(const CallInst &I);
|
|
|
|
void visitVAArg(const VAArgInst &I);
|
|
|
|
void visitVAEnd(const CallInst &I);
|
|
|
|
void visitVACopy(const CallInst &I);
|
2013-11-01 01:18:24 +08:00
|
|
|
void visitStackmap(const CallInst &I);
|
|
|
|
void visitPatchpoint(const CallInst &I);
|
2010-04-15 09:51:59 +08:00
|
|
|
|
|
|
|
void visitUserOp1(const Instruction &I) {
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("UserOp1 should not exist at instruction selection time!");
|
2008-09-04 00:12:24 +08:00
|
|
|
}
|
2010-04-15 09:51:59 +08:00
|
|
|
void visitUserOp2(const Instruction &I) {
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("UserOp2 should not exist at instruction selection time!");
|
2008-09-04 00:12:24 +08:00
|
|
|
}
|
2010-04-23 04:55:53 +08:00
|
|
|
|
2013-08-16 18:55:47 +08:00
|
|
|
void processIntegerCallValue(const Instruction &I,
|
|
|
|
SDValue Value, bool IsSigned);
|
|
|
|
|
2010-04-23 04:55:53 +08:00
|
|
|
void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
|
2010-04-29 07:08:54 +08:00
|
|
|
|
2010-08-26 04:41:24 +08:00
|
|
|
/// EmitFuncArgumentDbgValue - If V is an function argument then create
|
2013-11-01 01:18:07 +08:00
|
|
|
/// corresponding DBG_VALUE machine instruction for it now. At the end of
|
2010-08-26 04:41:24 +08:00
|
|
|
/// instruction selection, they will be inserted to the entry BB.
|
2010-08-26 04:39:26 +08:00
|
|
|
bool EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
|
2010-08-31 14:12:08 +08:00
|
|
|
int64_t Offset, const SDValue &N);
|
2008-09-04 00:12:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|