FunctionLoweringInfo.cpp revision 3f1403f14e45a862ff97c14d80a1daf28518722a
16277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman//===-- FunctionLoweringInfo.cpp ------------------------------------------===// 26277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// 36277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// The LLVM Compiler Infrastructure 46277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// 56277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// This file is distributed under the University of Illinois Open Source 66277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// License. See LICENSE.TXT for details. 76277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// 86277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman//===----------------------------------------------------------------------===// 96277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// 106277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// This implements routines for translating functions from LLVM IR into 116277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// Machine IR. 126277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman// 136277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman//===----------------------------------------------------------------------===// 146277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 156277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#define DEBUG_TYPE "function-lowering-info" 166277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "FunctionLoweringInfo.h" 176277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/CallingConv.h" 186277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/DerivedTypes.h" 196277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Function.h" 206277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Instructions.h" 215fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman#include "llvm/IntrinsicInst.h" 226277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/LLVMContext.h" 236277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Module.h" 246277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/CodeGen/MachineFunction.h" 256277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/CodeGen/MachineFrameInfo.h" 266277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/CodeGen/MachineInstrBuilder.h" 276277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/CodeGen/MachineModuleInfo.h" 286277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h" 296277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Analysis/DebugInfo.h" 306277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Target/TargetRegisterInfo.h" 316277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Target/TargetData.h" 326277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Target/TargetFrameInfo.h" 336277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Target/TargetInstrInfo.h" 34551754c4958086cc6910da7c950f2875e212f5cfEric Christopher#include "llvm/Target/TargetIntrinsicInfo.h" 356277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Target/TargetLowering.h" 366277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Target/TargetOptions.h" 376277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Support/Compiler.h" 386277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Support/Debug.h" 396277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Support/ErrorHandling.h" 406277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Support/MathExtras.h" 416277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include "llvm/Support/raw_ostream.h" 426277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#include <algorithm> 436277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohmanusing namespace llvm; 446277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 456277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence 466277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// of insertvalue or extractvalue indices that identify a member, return 476277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// the linearized index of the start of the member. 486277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// 496277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohmanunsigned llvm::ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty, 506277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const unsigned *Indices, 516277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const unsigned *IndicesEnd, 526277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned CurIndex) { 536277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Base case: We're done. 546277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (Indices && Indices == IndicesEnd) 556277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return CurIndex; 566277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 576277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Given a struct type, recursively traverse the elements. 586277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (const StructType *STy = dyn_cast<StructType>(Ty)) { 596277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (StructType::element_iterator EB = STy->element_begin(), 606277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EI = EB, 616277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EE = STy->element_end(); 626277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EI != EE; ++EI) { 636277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (Indices && *Indices == unsigned(EI - EB)) 646277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex); 656277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex); 666277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 676277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return CurIndex; 686277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 696277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Given an array type, recursively traverse the elements. 706277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 716277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const Type *EltTy = ATy->getElementType(); 726277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { 736277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (Indices && *Indices == i) 746277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex); 756277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex); 766277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 776277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return CurIndex; 786277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 796277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // We haven't found the type we're looking for, so keep searching. 806277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return CurIndex + 1; 816277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 826277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 836277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of 846277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// EVTs that represent all the individual underlying 856277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// non-aggregate types that comprise it. 866277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// 876277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// If Offsets is non-null, it points to a vector to be filled in 886277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// with the in-memory offsets of each of the individual values. 896277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// 906277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohmanvoid llvm::ComputeValueVTs(const TargetLowering &TLI, const Type *Ty, 916277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman SmallVectorImpl<EVT> &ValueVTs, 926277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman SmallVectorImpl<uint64_t> *Offsets, 936277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman uint64_t StartingOffset) { 946277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Given a struct type, recursively traverse the elements. 956277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (const StructType *STy = dyn_cast<StructType>(Ty)) { 966277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy); 976277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (StructType::element_iterator EB = STy->element_begin(), 986277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EI = EB, 996277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EE = STy->element_end(); 1006277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EI != EE; ++EI) 1016277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, 1026277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman StartingOffset + SL->getElementOffset(EI - EB)); 1036277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return; 1046277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 1056277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Given an array type, recursively traverse the elements. 1066277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 1076277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const Type *EltTy = ATy->getElementType(); 1086277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy); 1096277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) 1106277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, 1116277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman StartingOffset + i * EltSize); 1126277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return; 1136277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 1146277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Interpret void as zero return values. 115f012705c7e4ca8cf90b6b734ce1d5355daca5ba5Benjamin Kramer if (Ty->isVoidTy()) 1166277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return; 1176277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Base case: we can get an EVT for this LLVM IR type. 1186277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman ValueVTs.push_back(TLI.getValueType(Ty)); 1196277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (Offsets) 1206277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman Offsets->push_back(StartingOffset); 1216277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 1226277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1236277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by 1246277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// PHI nodes or outside of the basic block that defines it, or used by a 1256277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// switch or atomic instruction, which may expand to multiple basic blocks. 126ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohmanstatic bool isUsedOutsideOfDefiningBlock(const Instruction *I) { 1276277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (isa<PHINode>(I)) return true; 128ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman const BasicBlock *BB = I->getParent(); 129ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman for (Value::const_use_iterator UI = I->use_begin(), E = I->use_end(); 130ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman UI != E; ++UI) 1316277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) 1326277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return true; 1336277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return false; 1346277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 1356277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1366277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// isOnlyUsedInEntryBlock - If the specified argument is only used in the 1376277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// entry block, return true. This includes arguments used by switches, since 1386277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// the switch may expand into multiple basic blocks. 139ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohmanstatic bool isOnlyUsedInEntryBlock(const Argument *A, bool EnableFastISel) { 1406277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // With FastISel active, we may be splitting blocks, so force creation 1416277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // of virtual registers for all non-dead arguments. 1426277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Don't force virtual registers for byval arguments though, because 1436277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // fast-isel can't handle those in all cases. 1446277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (EnableFastISel && !A->hasByValAttr()) 1456277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return A->use_empty(); 1466277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 147ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman const BasicBlock *Entry = A->getParent()->begin(); 148ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); 149ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman UI != E; ++UI) 1506277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) 1516277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return false; // Use not in entry block. 1526277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return true; 1536277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 1546277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 155d858e90f039f5fcdc2fa93035e911a5a9505cc50Dan GohmanFunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli) 1566277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman : TLI(tli) { 1576277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 1586277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 159ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohmanvoid FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, 1606277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman bool EnableFastISel) { 1616277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman Fn = &fn; 1626277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MF = &mf; 1636277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman RegInfo = &MF->getRegInfo(); 1646277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1656277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Create a vreg for each argument register that is not dead and is used 1666277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // outside of the entry block for the function. 167ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman for (Function::const_arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); 1686277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman AI != E; ++AI) 1696277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) 1706277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman InitializeRegForValue(AI); 1716277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1726277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Initialize the mapping of values to registers. This is only set up for 1736277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // instruction values that are used outside of the block that defines 1746277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // them. 175ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman Function::const_iterator BB = Fn->begin(), EB = Fn->end(); 176ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) 177ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) 178ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { 1796277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const Type *Ty = AI->getAllocatedType(); 1806277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); 1816277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned Align = 1826277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), 1836277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman AI->getAlignment()); 1846277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1856277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman TySize *= CUI->getZExtValue(); // Get total allocated size. 1866277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. 1876277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman StaticAllocaMap[AI] = 1886277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MF->getFrameInfo()->CreateStackObject(TySize, Align, false); 1896277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 1906277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1916277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (; BB != EB; ++BB) 192ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) 1936277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) 1946277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (!isa<AllocaInst>(I) || 1956277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman !StaticAllocaMap.count(cast<AllocaInst>(I))) 1966277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman InitializeRegForValue(I); 1976277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 1986277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This 1996277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // also creates the initial PHI MachineInstrs, though none of the input 2006277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // operands are populated. 201d0d8275cb264b17bed836ccd35c923476a236426Dan Gohman for (BB = Fn->begin(); BB != EB; ++BB) { 2026277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); 2036277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MBBMap[BB] = MBB; 2046277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MF->push_back(MBB); 2056277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2066277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Transfer the address-taken flag. This is necessary because there could 2076277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only 2086277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // the first one should be marked. 2096277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (BB->hasAddressTaken()) 2106277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MBB->setHasAddressTaken(); 2116277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2126277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // Create Machine PHI nodes for LLVM PHI nodes, lowering them as 2136277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman // appropriate. 2146277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman DebugLoc DL; 2153f1403f14e45a862ff97c14d80a1daf28518722aDan Gohman for (BasicBlock::const_iterator I = BB->begin(); 2163f1403f14e45a862ff97c14d80a1daf28518722aDan Gohman const PHINode *PN = dyn_cast<PHINode>(I); ++I) { 2173f1403f14e45a862ff97c14d80a1daf28518722aDan Gohman if (PN->use_empty()) continue; 2186277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2196277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned PHIReg = ValueMap[PN]; 2206277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman assert(PHIReg && "PHI node does not have an assigned virtual register!"); 2216277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2226277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman SmallVector<EVT, 4> ValueVTs; 2236277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman ComputeValueVTs(TLI, PN->getType(), ValueVTs); 2246277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { 2256277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EVT VT = ValueVTs[vti]; 2266277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT); 2276277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 2286277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (unsigned i = 0; i != NumRegisters; ++i) 229518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); 2306277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman PHIReg += NumRegisters; 2316277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 2326277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 2336277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 234de4c0a7da7cfa1693ad9e6e09969003b610af08aDan Gohman 235de4c0a7da7cfa1693ad9e6e09969003b610af08aDan Gohman // Mark landing pad blocks. 236de4c0a7da7cfa1693ad9e6e09969003b610af08aDan Gohman for (BB = Fn->begin(); BB != EB; ++BB) 237ae541aad5c36cb3e4256514447d1f81e253079c7Dan Gohman if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) 238de4c0a7da7cfa1693ad9e6e09969003b610af08aDan Gohman MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); 2396277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 2406277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2416277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// clear - Clear out all the function-specific state. This returns this 2426277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// FunctionLoweringInfo to an empty state, ready to be used for a 2436277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// different function. 2446277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohmanvoid FunctionLoweringInfo::clear() { 2450e026729aef35523e4f8d6124a569dceb4286bbbDan Gohman assert(CatchInfoFound.size() == CatchInfoLost.size() && 2460e026729aef35523e4f8d6124a569dceb4286bbbDan Gohman "Not all catch info was assigned to a landing pad!"); 2470e026729aef35523e4f8d6124a569dceb4286bbbDan Gohman 2486277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman MBBMap.clear(); 2496277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman ValueMap.clear(); 2506277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman StaticAllocaMap.clear(); 2516277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#ifndef NDEBUG 2526277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman CatchInfoLost.clear(); 2536277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman CatchInfoFound.clear(); 2546277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman#endif 2556277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman LiveOutRegInfo.clear(); 2566277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 2576277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2586277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohmanunsigned FunctionLoweringInfo::MakeReg(EVT VT) { 2596277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); 2606277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 2616277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2626277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// CreateRegForValue - Allocate the appropriate number of virtual registers of 2636277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// the correctly promoted or expanded types. Assign these registers 2646277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// consecutive vreg numbers and return the first assigned number. 2656277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// 2666277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// In the case that the given value has struct or array type, this function 2676277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// will assign registers for each member or element. 2686277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman/// 2696277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohmanunsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) { 2706277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman SmallVector<EVT, 4> ValueVTs; 2716277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman ComputeValueVTs(TLI, V->getType(), ValueVTs); 2726277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2736277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned FirstReg = 0; 2746277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { 2756277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EVT ValueVT = ValueVTs[Value]; 2766277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT); 2776277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman 2786277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT); 2796277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman for (unsigned i = 0; i != NumRegs; ++i) { 2806277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman unsigned R = MakeReg(RegisterVT); 2816277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman if (!FirstReg) FirstReg = R; 2826277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 2836277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman } 2846277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman return FirstReg; 2856277eb2bb997a5da0808a8a5a57f18fd9faaf336Dan Gohman} 28666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 28766336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. 28866336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan GohmanGlobalVariable *llvm::ExtractTypeInfo(Value *V) { 28966336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman V = V->stripPointerCasts(); 29066336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman GlobalVariable *GV = dyn_cast<GlobalVariable>(V); 291e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling 292e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling if (GV && GV->getName() == ".llvm.eh.catch.all.value") { 293e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling assert(GV->hasInitializer() && 294e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling "The EH catch-all value must have an initializer"); 295e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling Value *Init = GV->getInitializer(); 296e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling GV = dyn_cast<GlobalVariable>(Init); 297e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling if (!GV) V = cast<ConstantPointerNull>(Init); 298e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling } 299e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling 300e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling assert((GV || isa<ConstantPointerNull>(V)) && 301e6b293b78d8eb3eb749771d2201ace3ecd076010Bill Wendling "TypeInfo must be a global variable or NULL"); 30266336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman return GV; 30366336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman} 30466336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 30566336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman/// AddCatchInfo - Extract the personality and type infos from an eh.selector 30666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman/// call, and add them to the specified machine basic block. 3072520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohmanvoid llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI, 30866336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman MachineBasicBlock *MBB) { 30966336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman // Inform the MachineModuleInfo of the personality for this landing pad. 310551754c4958086cc6910da7c950f2875e212f5cfEric Christopher const ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2)); 31166336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman assert(CE->getOpcode() == Instruction::BitCast && 31266336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman isa<Function>(CE->getOperand(0)) && 31366336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman "Personality should be a function"); 31466336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); 31566336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 31666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman // Gather all the type infos for this landing pad and pass them along to 31766336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman // MachineModuleInfo. 31846510a73e977273ec67747eb34cbdb43f815e451Dan Gohman std::vector<const GlobalVariable *> TyInfo; 319551754c4958086cc6910da7c950f2875e212f5cfEric Christopher unsigned N = I.getNumOperands(); 32066336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 321551754c4958086cc6910da7c950f2875e212f5cfEric Christopher for (unsigned i = N - 1; i > 2; --i) { 3222520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohman if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) { 32366336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman unsigned FilterLength = CI->getZExtValue(); 32466336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman unsigned FirstCatch = i + FilterLength + !FilterLength; 32566336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman assert (FirstCatch <= N && "Invalid filter length"); 32666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 32766336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman if (FirstCatch < N) { 32866336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.reserve(N - FirstCatch); 32966336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman for (unsigned j = FirstCatch; j < N; ++j) 33066336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); 33166336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman MMI->addCatchTypeInfo(MBB, TyInfo); 33266336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.clear(); 33366336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman } 33466336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 33566336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman if (!FilterLength) { 33666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman // Cleanup. 33766336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman MMI->addCleanup(MBB); 33866336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman } else { 33966336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman // Filter. 34066336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.reserve(FilterLength - 1); 34166336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman for (unsigned j = i + 1; j < FirstCatch; ++j) 34266336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); 34366336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman MMI->addFilterTypeInfo(MBB, TyInfo); 34466336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.clear(); 34566336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman } 34666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 34766336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman N = i; 34866336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman } 34966336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman } 35066336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 351551754c4958086cc6910da7c950f2875e212f5cfEric Christopher if (N > 3) { 352551754c4958086cc6910da7c950f2875e212f5cfEric Christopher TyInfo.reserve(N - 3); 353551754c4958086cc6910da7c950f2875e212f5cfEric Christopher for (unsigned j = 3; j < N; ++j) 35466336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman TyInfo.push_back(ExtractTypeInfo(I.getOperand(j))); 35566336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman MMI->addCatchTypeInfo(MBB, TyInfo); 35666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman } 35766336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman} 35866336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman 3592520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohmanvoid llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB, 3605fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) { 3612520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohman for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end(); 3622520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohman I != E; ++I) 3632520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohman if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) { 3645fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman // Apply the catch info to DestBB. 3655fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]); 3665fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman#ifndef NDEBUG 3675fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman if (!FLI.MBBMap[SrcBB]->isLandingPad()) 3685fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman FLI.CatchInfoFound.insert(EHSel); 3695fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman#endif 3705fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman } 3715fca8b1c8dc0b9efb0a9822222a6505d1eea6bddDan Gohman} 372fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman 373fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being 374fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// processed uses a memory 'm' constraint. 375fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohmanbool 376fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohmanllvm::hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos, 377fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman const TargetLowering &TLI) { 378fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { 379fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman InlineAsm::ConstraintInfo &CI = CInfos[i]; 380fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { 381fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); 382fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman if (CType == TargetLowering::C_Memory) 383fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman return true; 384fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman } 385fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman 386fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman // Indirect operand accesses access memory. 387fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman if (CI.isIndirect) 388fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman return true; 389fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman } 390fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman 391fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman return false; 392fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman} 393fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman 394fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// getFCmpCondCode - Return the ISD condition code corresponding to 395fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// the given LLVM IR floating-point condition code. This includes 396fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// consideration of global floating-point math flags. 397fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// 398fe85e764736f4d87104f3d1508c173f566c8c461Dan GohmanISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) { 399fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman ISD::CondCode FPC, FOC; 400fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman switch (Pred) { 401fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break; 402fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break; 403fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break; 404fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break; 405fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break; 406fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break; 407fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break; 408fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break; 409fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break; 410fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break; 411fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break; 412fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break; 413fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break; 414fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break; 415fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break; 416fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break; 417fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman default: 418fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman llvm_unreachable("Invalid FCmp predicate opcode!"); 419fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman FOC = FPC = ISD::SETFALSE; 420fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman break; 421fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman } 422fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman if (FiniteOnlyFPMath()) 423fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman return FOC; 424fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman else 425fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman return FPC; 426fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman} 427fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman 428fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// getICmpCondCode - Return the ISD condition code corresponding to 429fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// the given LLVM IR integer condition code. 430fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman/// 431fe85e764736f4d87104f3d1508c173f566c8c461Dan GohmanISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) { 432fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman switch (Pred) { 433fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_EQ: return ISD::SETEQ; 434fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_NE: return ISD::SETNE; 435fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_SLE: return ISD::SETLE; 436fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_ULE: return ISD::SETULE; 437fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_SGE: return ISD::SETGE; 438fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_UGE: return ISD::SETUGE; 439fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_SLT: return ISD::SETLT; 440fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_ULT: return ISD::SETULT; 441fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_SGT: return ISD::SETGT; 442fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman case ICmpInst::ICMP_UGT: return ISD::SETUGT; 443fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman default: 444fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman llvm_unreachable("Invalid ICmp predicate opcode!"); 445fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman return ISD::SETNE; 446fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman } 447fe85e764736f4d87104f3d1508c173f566c8c461Dan Gohman} 44846007b3712290c09d895f4bd245ac852f412556cDan Gohman 44946007b3712290c09d895f4bd245ac852f412556cDan Gohman/// Test if the given instruction is in a position to be optimized 45046007b3712290c09d895f4bd245ac852f412556cDan Gohman/// with a tail-call. This roughly means that it's in a block with 45146007b3712290c09d895f4bd245ac852f412556cDan Gohman/// a return and there's nothing that needs to be scheduled 45246007b3712290c09d895f4bd245ac852f412556cDan Gohman/// between it and the return. 45346007b3712290c09d895f4bd245ac852f412556cDan Gohman/// 45446007b3712290c09d895f4bd245ac852f412556cDan Gohman/// This function only tests target-independent requirements. 45546007b3712290c09d895f4bd245ac852f412556cDan Gohmanbool llvm::isInTailCallPosition(ImmutableCallSite CS, Attributes CalleeRetAttr, 45646007b3712290c09d895f4bd245ac852f412556cDan Gohman const TargetLowering &TLI) { 45746007b3712290c09d895f4bd245ac852f412556cDan Gohman const Instruction *I = CS.getInstruction(); 45846007b3712290c09d895f4bd245ac852f412556cDan Gohman const BasicBlock *ExitBB = I->getParent(); 45946007b3712290c09d895f4bd245ac852f412556cDan Gohman const TerminatorInst *Term = ExitBB->getTerminator(); 46046007b3712290c09d895f4bd245ac852f412556cDan Gohman const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); 46146007b3712290c09d895f4bd245ac852f412556cDan Gohman const Function *F = ExitBB->getParent(); 46246007b3712290c09d895f4bd245ac852f412556cDan Gohman 46346007b3712290c09d895f4bd245ac852f412556cDan Gohman // The block must end in a return statement or unreachable. 46446007b3712290c09d895f4bd245ac852f412556cDan Gohman // 46546007b3712290c09d895f4bd245ac852f412556cDan Gohman // FIXME: Decline tailcall if it's not guaranteed and if the block ends in 46646007b3712290c09d895f4bd245ac852f412556cDan Gohman // an unreachable, for now. The way tailcall optimization is currently 46746007b3712290c09d895f4bd245ac852f412556cDan Gohman // implemented means it will add an epilogue followed by a jump. That is 46846007b3712290c09d895f4bd245ac852f412556cDan Gohman // not profitable. Also, if the callee is a special function (e.g. 46946007b3712290c09d895f4bd245ac852f412556cDan Gohman // longjmp on x86), it can end up causing miscompilation that has not 47046007b3712290c09d895f4bd245ac852f412556cDan Gohman // been fully understood. 47146007b3712290c09d895f4bd245ac852f412556cDan Gohman if (!Ret && 47246007b3712290c09d895f4bd245ac852f412556cDan Gohman (!GuaranteedTailCallOpt || !isa<UnreachableInst>(Term))) return false; 47346007b3712290c09d895f4bd245ac852f412556cDan Gohman 47446007b3712290c09d895f4bd245ac852f412556cDan Gohman // If I will have a chain, make sure no other instruction that will have a 47546007b3712290c09d895f4bd245ac852f412556cDan Gohman // chain interposes between I and the return. 47646007b3712290c09d895f4bd245ac852f412556cDan Gohman if (I->mayHaveSideEffects() || I->mayReadFromMemory() || 47746007b3712290c09d895f4bd245ac852f412556cDan Gohman !I->isSafeToSpeculativelyExecute()) 47846007b3712290c09d895f4bd245ac852f412556cDan Gohman for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; 47946007b3712290c09d895f4bd245ac852f412556cDan Gohman --BBI) { 48046007b3712290c09d895f4bd245ac852f412556cDan Gohman if (&*BBI == I) 48146007b3712290c09d895f4bd245ac852f412556cDan Gohman break; 48246007b3712290c09d895f4bd245ac852f412556cDan Gohman // Debug info intrinsics do not get in the way of tail call optimization. 48346007b3712290c09d895f4bd245ac852f412556cDan Gohman if (isa<DbgInfoIntrinsic>(BBI)) 48446007b3712290c09d895f4bd245ac852f412556cDan Gohman continue; 48546007b3712290c09d895f4bd245ac852f412556cDan Gohman if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || 48646007b3712290c09d895f4bd245ac852f412556cDan Gohman !BBI->isSafeToSpeculativelyExecute()) 48746007b3712290c09d895f4bd245ac852f412556cDan Gohman return false; 48846007b3712290c09d895f4bd245ac852f412556cDan Gohman } 48946007b3712290c09d895f4bd245ac852f412556cDan Gohman 49046007b3712290c09d895f4bd245ac852f412556cDan Gohman // If the block ends with a void return or unreachable, it doesn't matter 49146007b3712290c09d895f4bd245ac852f412556cDan Gohman // what the call's return type is. 49246007b3712290c09d895f4bd245ac852f412556cDan Gohman if (!Ret || Ret->getNumOperands() == 0) return true; 49346007b3712290c09d895f4bd245ac852f412556cDan Gohman 49446007b3712290c09d895f4bd245ac852f412556cDan Gohman // If the return value is undef, it doesn't matter what the call's 49546007b3712290c09d895f4bd245ac852f412556cDan Gohman // return type is. 49646007b3712290c09d895f4bd245ac852f412556cDan Gohman if (isa<UndefValue>(Ret->getOperand(0))) return true; 49746007b3712290c09d895f4bd245ac852f412556cDan Gohman 49846007b3712290c09d895f4bd245ac852f412556cDan Gohman // Conservatively require the attributes of the call to match those of 49946007b3712290c09d895f4bd245ac852f412556cDan Gohman // the return. Ignore noalias because it doesn't affect the call sequence. 50046007b3712290c09d895f4bd245ac852f412556cDan Gohman unsigned CallerRetAttr = F->getAttributes().getRetAttributes(); 50146007b3712290c09d895f4bd245ac852f412556cDan Gohman if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias) 50246007b3712290c09d895f4bd245ac852f412556cDan Gohman return false; 50346007b3712290c09d895f4bd245ac852f412556cDan Gohman 50446007b3712290c09d895f4bd245ac852f412556cDan Gohman // It's not safe to eliminate the sign / zero extension of the return value. 50546007b3712290c09d895f4bd245ac852f412556cDan Gohman if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt)) 50646007b3712290c09d895f4bd245ac852f412556cDan Gohman return false; 50746007b3712290c09d895f4bd245ac852f412556cDan Gohman 50846007b3712290c09d895f4bd245ac852f412556cDan Gohman // Otherwise, make sure the unmodified return value of I is the return value. 50946007b3712290c09d895f4bd245ac852f412556cDan Gohman for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ; 51046007b3712290c09d895f4bd245ac852f412556cDan Gohman U = dyn_cast<Instruction>(U->getOperand(0))) { 51146007b3712290c09d895f4bd245ac852f412556cDan Gohman if (!U) 51246007b3712290c09d895f4bd245ac852f412556cDan Gohman return false; 51346007b3712290c09d895f4bd245ac852f412556cDan Gohman if (!U->hasOneUse()) 51446007b3712290c09d895f4bd245ac852f412556cDan Gohman return false; 51546007b3712290c09d895f4bd245ac852f412556cDan Gohman if (U == I) 51646007b3712290c09d895f4bd245ac852f412556cDan Gohman break; 51746007b3712290c09d895f4bd245ac852f412556cDan Gohman // Check for a truly no-op truncate. 51846007b3712290c09d895f4bd245ac852f412556cDan Gohman if (isa<TruncInst>(U) && 51946007b3712290c09d895f4bd245ac852f412556cDan Gohman TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType())) 52046007b3712290c09d895f4bd245ac852f412556cDan Gohman continue; 52146007b3712290c09d895f4bd245ac852f412556cDan Gohman // Check for a truly no-op bitcast. 52246007b3712290c09d895f4bd245ac852f412556cDan Gohman if (isa<BitCastInst>(U) && 52346007b3712290c09d895f4bd245ac852f412556cDan Gohman (U->getOperand(0)->getType() == U->getType() || 52446007b3712290c09d895f4bd245ac852f412556cDan Gohman (U->getOperand(0)->getType()->isPointerTy() && 52546007b3712290c09d895f4bd245ac852f412556cDan Gohman U->getType()->isPointerTy()))) 52646007b3712290c09d895f4bd245ac852f412556cDan Gohman continue; 52746007b3712290c09d895f4bd245ac852f412556cDan Gohman // Otherwise it's not a true no-op. 52846007b3712290c09d895f4bd245ac852f412556cDan Gohman return false; 52946007b3712290c09d895f4bd245ac852f412556cDan Gohman } 53046007b3712290c09d895f4bd245ac852f412556cDan Gohman 53146007b3712290c09d895f4bd245ac852f412556cDan Gohman return true; 53246007b3712290c09d895f4bd245ac852f412556cDan Gohman} 533