Chris Lattner | 8494d08 | 2002-10-28 01:16:38 +0000 | [diff] [blame] | 1 | //===-- MachineFunction.cpp -----------------------------------------------===// |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 8494d08 | 2002-10-28 01:16:38 +0000 | [diff] [blame] | 10 | // Collect native machine code information for a function. This allows |
| 11 | // target-specific information about the generated code to be stored with each |
| 12 | // function. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Chris Lattner | eda6bd7 | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 15 | |
David Greene | 8230a93 | 2009-08-19 22:16:11 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallString.h" |
| 19 | #include "llvm/Analysis/ConstantFolding.h" |
David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/EHPersonalities.h" |
Chris Lattner | e32fad4 | 2012-01-27 01:47:28 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionInitializer.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 9f53dcd | 2010-04-05 05:49:50 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | 4cbb97b | 2003-12-20 10:20:58 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/Passes.h" |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Function.h" |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
Duncan P. N. Exon Smith | 32692154 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 36 | #include "llvm/IR/ModuleSlotTracker.h" |
Chris Lattner | 273735b | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCAsmInfo.h" |
| 38 | #include "llvm/MC/MCContext.h" |
David Greene | 6671011 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Chris Lattner | bd7286e | 2006-10-03 20:19:23 +0000 | [diff] [blame] | 40 | #include "llvm/Support/GraphWriter.h" |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetFrameLowering.h" |
| 43 | #include "llvm/Target/TargetLowering.h" |
| 44 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chris Lattner | 9a3478e | 2003-12-20 09:17:07 +0000 | [diff] [blame] | 46 | using namespace llvm; |
Chris Lattner | eda6bd7 | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 47 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 48 | #define DEBUG_TYPE "codegen" |
| 49 | |
Chad Rosier | 6b43263 | 2015-12-29 18:18:07 +0000 | [diff] [blame] | 50 | static cl::opt<unsigned> |
| 51 | AlignAllFunctions("align-all-functions", |
| 52 | cl::desc("Force the alignment of all functions."), |
| 53 | cl::init(0), cl::Hidden); |
| 54 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 55 | void MachineFunctionInitializer::anchor() {} |
| 56 | |
Derek Schuff | 025191d | 2016-04-21 22:19:24 +0000 | [diff] [blame] | 57 | void MachineFunctionProperties::print(raw_ostream &ROS, bool OnlySet) const { |
Derek Schuff | 07636cd | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 58 | // Leave this function even in NDEBUG as an out-of-line anchor. |
| 59 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Derek Schuff | 07636cd | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 60 | for (BitVector::size_type i = 0; i < Properties.size(); ++i) { |
Derek Schuff | 73900c6 | 2016-04-04 18:03:29 +0000 | [diff] [blame] | 61 | bool HasProperty = Properties[i]; |
Derek Schuff | 025191d | 2016-04-21 22:19:24 +0000 | [diff] [blame] | 62 | if (OnlySet && !HasProperty) |
| 63 | continue; |
Derek Schuff | 73900c6 | 2016-04-04 18:03:29 +0000 | [diff] [blame] | 64 | switch(static_cast<Property>(i)) { |
| 65 | case Property::IsSSA: |
| 66 | ROS << (HasProperty ? "SSA, " : "Post SSA, "); |
| 67 | break; |
Derek Schuff | f7b2bce | 2016-04-11 23:32:13 +0000 | [diff] [blame] | 68 | case Property::TracksLiveness: |
| 69 | ROS << (HasProperty ? "" : "not ") << "tracking liveness, "; |
| 70 | break; |
Derek Schuff | 73900c6 | 2016-04-04 18:03:29 +0000 | [diff] [blame] | 71 | case Property::AllVRegsAllocated: |
| 72 | ROS << (HasProperty ? "AllVRegsAllocated" : "HasVRegs"); |
| 73 | break; |
| 74 | default: |
| 75 | break; |
Derek Schuff | 07636cd | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | #endif |
| 79 | } |
| 80 | |
Chris Lattner | 6715952 | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 81 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6d8a6c6 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 82 | // MachineFunction implementation |
Chris Lattner | 6715952 | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
Chris Lattner | e6074aa | 2005-01-29 18:41:25 +0000 | [diff] [blame] | 84 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 85 | // Out-of-line virtual method. |
Chris Lattner | f2471ec | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 86 | MachineFunctionInfo::~MachineFunctionInfo() {} |
| 87 | |
Dan Gohman | 804c95d | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 88 | void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 89 | MBB->getParent()->DeleteMachineBasicBlock(MBB); |
Tanya Lattner | a578cb7 | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 6d8a6c6 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 91 | |
Charles Davis | 2f65f35 | 2016-04-09 23:34:42 +0000 | [diff] [blame] | 92 | static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, |
| 93 | const Function *Fn) { |
| 94 | if (Fn->hasFnAttribute(Attribute::StackAlignment)) |
| 95 | return Fn->getFnStackAlignment(); |
| 96 | return STI->getFrameLowering()->getStackAlignment(); |
| 97 | } |
| 98 | |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 99 | MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM, |
Rafael Espindola | 76936eb | 2014-10-14 18:53:16 +0000 | [diff] [blame] | 100 | unsigned FunctionNum, MachineModuleInfo &mmi) |
Eric Christopher | c5a85af | 2015-03-21 03:13:10 +0000 | [diff] [blame] | 101 | : Fn(F), Target(TM), STI(TM.getSubtargetImpl(*F)), Ctx(mmi.getContext()), |
Rafael Espindola | 76936eb | 2014-10-14 18:53:16 +0000 | [diff] [blame] | 102 | MMI(mmi) { |
Derek Schuff | f7b2bce | 2016-04-11 23:32:13 +0000 | [diff] [blame] | 103 | // Assume the function starts in SSA form with correct liveness. |
Derek Schuff | 73900c6 | 2016-04-04 18:03:29 +0000 | [diff] [blame] | 104 | Properties.set(MachineFunctionProperties::Property::IsSSA); |
Derek Schuff | f7b2bce | 2016-04-11 23:32:13 +0000 | [diff] [blame] | 105 | Properties.set(MachineFunctionProperties::Property::TracksLiveness); |
Eric Christopher | 51bedaf | 2014-10-08 07:51:41 +0000 | [diff] [blame] | 106 | if (STI->getRegisterInfo()) |
Eric Christopher | ce40dbc | 2014-08-12 08:00:56 +0000 | [diff] [blame] | 107 | RegInfo = new (Allocator) MachineRegisterInfo(this); |
Matthijs Kooijman | c8d7988 | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 108 | else |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 109 | RegInfo = nullptr; |
Bill Wendling | 626c991 | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 110 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 111 | MFInfo = nullptr; |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 112 | // We can realign the stack if the target supports it and the user hasn't |
| 113 | // explicitly asked us not to. |
| 114 | bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && |
| 115 | !F->hasFnAttribute("no-realign-stack"); |
| 116 | FrameInfo = new (Allocator) MachineFrameInfo( |
| 117 | getFnStackAlignment(STI, Fn), /*StackRealignable=*/CanRealignSP, |
| 118 | /*ForceRealign=*/CanRealignSP && |
| 119 | F->hasFnAttribute(Attribute::StackAlignment)); |
Bill Wendling | 626c991 | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 120 | |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 121 | if (Fn->hasFnAttribute(Attribute::StackAlignment)) |
| 122 | FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment()); |
Bill Wendling | 626c991 | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 123 | |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 124 | ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); |
Eric Christopher | 51bedaf | 2014-10-08 07:51:41 +0000 | [diff] [blame] | 125 | Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); |
Bill Wendling | 626c991 | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 126 | |
Eli Friedman | 2518f83 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 127 | // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn. |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 128 | // FIXME: Use Function::optForSize(). |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 129 | if (!Fn->hasFnAttribute(Attribute::OptimizeForSize)) |
Eric Christopher | 51bedaf | 2014-10-08 07:51:41 +0000 | [diff] [blame] | 130 | Alignment = std::max(Alignment, |
| 131 | STI->getTargetLowering()->getPrefFunctionAlignment()); |
Bill Wendling | 626c991 | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 132 | |
Chad Rosier | 6b43263 | 2015-12-29 18:18:07 +0000 | [diff] [blame] | 133 | if (AlignAllFunctions) |
| 134 | Alignment = AlignAllFunctions; |
| 135 | |
Chris Lattner | 6715952 | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 136 | FunctionNumber = FunctionNum; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 137 | JumpTableInfo = nullptr; |
Tobias Grosser | 58fdd88 | 2015-08-17 10:58:03 +0000 | [diff] [blame] | 138 | |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 139 | if (isFuncletEHPersonality(classifyEHPersonality( |
| 140 | F->hasPersonalityFn() ? F->getPersonalityFn() : nullptr))) { |
| 141 | WinEHInfo = new (Allocator) WinEHFuncInfo(); |
| 142 | } |
| 143 | |
Tobias Grosser | 58fdd88 | 2015-08-17 10:58:03 +0000 | [diff] [blame] | 144 | assert(TM.isCompatibleDataLayout(getDataLayout()) && |
| 145 | "Can't create a MachineFunction using a Module with a " |
| 146 | "Target-incompatible DataLayout attached\n"); |
| 147 | |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 148 | PSVManager = llvm::make_unique<PseudoSourceValueManager>(); |
Chris Lattner | 448fb45 | 2002-12-25 05:03:22 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 151 | MachineFunction::~MachineFunction() { |
Jakob Stoklund Olesen | dc5285f | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 152 | // Don't call destructors on MachineInstr and MachineOperand. All of their |
| 153 | // memory comes from the BumpPtrAllocator which is about to be purged. |
| 154 | // |
| 155 | // Do call MachineBasicBlock destructors, it contains std::vectors. |
| 156 | for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) |
| 157 | I->Insts.clearAndLeakNodesUnsafely(); |
| 158 | |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 159 | InstructionRecycler.clear(Allocator); |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 160 | OperandRecycler.clear(Allocator); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 161 | BasicBlockRecycler.clear(Allocator); |
Bill Wendling | 7f7eb8a | 2009-06-25 00:32:48 +0000 | [diff] [blame] | 162 | if (RegInfo) { |
| 163 | RegInfo->~MachineRegisterInfo(); |
| 164 | Allocator.Deallocate(RegInfo); |
| 165 | } |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 166 | if (MFInfo) { |
Bill Wendling | 7f7eb8a | 2009-06-25 00:32:48 +0000 | [diff] [blame] | 167 | MFInfo->~MachineFunctionInfo(); |
| 168 | Allocator.Deallocate(MFInfo); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 169 | } |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 170 | |
| 171 | FrameInfo->~MachineFrameInfo(); |
| 172 | Allocator.Deallocate(FrameInfo); |
| 173 | |
| 174 | ConstantPool->~MachineConstantPool(); |
| 175 | Allocator.Deallocate(ConstantPool); |
| 176 | |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 177 | if (JumpTableInfo) { |
| 178 | JumpTableInfo->~MachineJumpTableInfo(); |
| 179 | Allocator.Deallocate(JumpTableInfo); |
| 180 | } |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 181 | |
| 182 | if (WinEHInfo) { |
| 183 | WinEHInfo->~WinEHFuncInfo(); |
| 184 | Allocator.Deallocate(WinEHInfo); |
| 185 | } |
Chris Lattner | 214808f | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 188 | const DataLayout &MachineFunction::getDataLayout() const { |
| 189 | return Fn->getParent()->getDataLayout(); |
| 190 | } |
| 191 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 192 | /// Get the JumpTableInfo for this function. |
| 193 | /// If it does not already exist, allocate one. |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 194 | MachineJumpTableInfo *MachineFunction:: |
| 195 | getOrCreateJumpTableInfo(unsigned EntryKind) { |
| 196 | if (JumpTableInfo) return JumpTableInfo; |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 197 | |
Dan Gohman | 01c65a2 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 198 | JumpTableInfo = new (Allocator) |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 199 | MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); |
| 200 | return JumpTableInfo; |
| 201 | } |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 202 | |
Reid Kleckner | 9c65821 | 2014-04-10 22:58:43 +0000 | [diff] [blame] | 203 | /// Should we be emitting segmented stack stuff for the function |
Quentin Colombet | 2c49e2e | 2016-01-19 22:31:12 +0000 | [diff] [blame] | 204 | bool MachineFunction::shouldSplitStack() const { |
Reid Kleckner | 9c65821 | 2014-04-10 22:58:43 +0000 | [diff] [blame] | 205 | return getFunction()->hasFnAttribute("split-stack"); |
| 206 | } |
| 207 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 208 | /// This discards all of the MachineBasicBlock numbers and recomputes them. |
| 209 | /// This guarantees that the MBB numbers are sequential, dense, and match the |
| 210 | /// ordering of the blocks within the function. If a specific MachineBasicBlock |
| 211 | /// is specified, only that block and those after it are renumbered. |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 212 | void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { |
| 213 | if (empty()) { MBBNumbering.clear(); return; } |
| 214 | MachineFunction::iterator MBBI, E = end(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 215 | if (MBB == nullptr) |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 216 | MBBI = begin(); |
| 217 | else |
Duncan P. N. Exon Smith | 5ec1568 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 218 | MBBI = MBB->getIterator(); |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 220 | // Figure out the block number this should have. |
| 221 | unsigned BlockNo = 0; |
Chris Lattner | bd7286e | 2006-10-03 20:19:23 +0000 | [diff] [blame] | 222 | if (MBBI != begin()) |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 223 | BlockNo = std::prev(MBBI)->getNumber() + 1; |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 225 | for (; MBBI != E; ++MBBI, ++BlockNo) { |
| 226 | if (MBBI->getNumber() != (int)BlockNo) { |
| 227 | // Remove use of the old number. |
| 228 | if (MBBI->getNumber() != -1) { |
| 229 | assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && |
| 230 | "MBB number mismatch!"); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 231 | MBBNumbering[MBBI->getNumber()] = nullptr; |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 232 | } |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 234 | // If BlockNo is already taken, set that block's number to -1. |
| 235 | if (MBBNumbering[BlockNo]) |
| 236 | MBBNumbering[BlockNo]->setNumber(-1); |
| 237 | |
Duncan P. N. Exon Smith | 5ec1568 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 238 | MBBNumbering[BlockNo] = &*MBBI; |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 239 | MBBI->setNumber(BlockNo); |
| 240 | } |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 241 | } |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 242 | |
| 243 | // Okay, all the blocks are renumbered. If we have compactified the block |
| 244 | // numbering, shrink MBBNumbering now. |
| 245 | assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); |
| 246 | MBBNumbering.resize(BlockNo); |
| 247 | } |
| 248 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 249 | /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 250 | MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, |
| 251 | const DebugLoc &DL, |
| 252 | bool NoImp) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 253 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
Jakob Stoklund Olesen | ac4210e | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 254 | MachineInstr(*this, MCID, DL, NoImp); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | 64fd948 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 256 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 257 | /// Create a new MachineInstr which is a copy of the 'Orig' instruction, |
| 258 | /// identical in all ways except the instruction has no parent, prev, or next. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 259 | MachineInstr * |
| 260 | MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { |
| 261 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
| 262 | MachineInstr(*this, *Orig); |
| 263 | } |
| 264 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 265 | /// Delete the given MachineInstr. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 266 | /// |
Jakob Stoklund Olesen | dc5285f | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 267 | /// This function also serves as the MachineInstr destructor - the real |
| 268 | /// ~MachineInstr() destructor must be empty. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 269 | void |
| 270 | MachineFunction::DeleteMachineInstr(MachineInstr *MI) { |
Jakob Stoklund Olesen | 1bfeecb | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 271 | // Strip it for parts. The operand array and the MI object itself are |
| 272 | // independently recyclable. |
| 273 | if (MI->Operands) |
| 274 | deallocateOperandArray(MI->CapOperands, MI->Operands); |
Jakob Stoklund Olesen | dc5285f | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 275 | // Don't call ~MachineInstr() which must be trivial anyway because |
| 276 | // ~MachineFunction drops whole lists of MachineInstrs wihout calling their |
| 277 | // destructors. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 278 | InstructionRecycler.Deallocate(Allocator, MI); |
| 279 | } |
| 280 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 281 | /// Allocate a new MachineBasicBlock. Use this instead of |
| 282 | /// `new MachineBasicBlock'. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 283 | MachineBasicBlock * |
| 284 | MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { |
| 285 | return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) |
| 286 | MachineBasicBlock(*this, bb); |
| 287 | } |
| 288 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 289 | /// Delete the given MachineBasicBlock. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 290 | void |
| 291 | MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { |
| 292 | assert(MBB->getParent() == this && "MBB parent mismatch!"); |
| 293 | MBB->~MachineBasicBlock(); |
| 294 | BasicBlockRecycler.Deallocate(Allocator, MBB); |
| 295 | } |
| 296 | |
Justin Lebar | 0af80cd | 2016-07-15 18:26:59 +0000 | [diff] [blame] | 297 | MachineMemOperand *MachineFunction::getMachineMemOperand( |
| 298 | MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, |
| 299 | unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges) { |
Justin Lebar | a3b786a | 2016-07-14 17:07:44 +0000 | [diff] [blame] | 300 | return new (Allocator) |
Justin Lebar | 0af80cd | 2016-07-15 18:26:59 +0000 | [diff] [blame] | 301 | MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges); |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | MachineMemOperand * |
| 305 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
| 306 | int64_t Offset, uint64_t Size) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 307 | if (MMO->getValue()) |
| 308 | return new (Allocator) |
| 309 | MachineMemOperand(MachinePointerInfo(MMO->getValue(), |
| 310 | MMO->getOffset()+Offset), |
| 311 | MMO->getFlags(), Size, |
Benjamin Kramer | 2e52f02 | 2014-10-04 22:44:29 +0000 | [diff] [blame] | 312 | MMO->getBaseAlignment()); |
Dan Gohman | 01c65a2 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 313 | return new (Allocator) |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 314 | MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(), |
Chris Lattner | 00ca0b8 | 2010-09-21 04:32:08 +0000 | [diff] [blame] | 315 | MMO->getOffset()+Offset), |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 316 | MMO->getFlags(), Size, |
Benjamin Kramer | 2e52f02 | 2014-10-04 22:44:29 +0000 | [diff] [blame] | 317 | MMO->getBaseAlignment()); |
Dan Gohman | 48b185d | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | MachineInstr::mmo_iterator |
| 321 | MachineFunction::allocateMemRefsArray(unsigned long Num) { |
| 322 | return Allocator.Allocate<MachineMemOperand *>(Num); |
| 323 | } |
| 324 | |
Dan Gohman | dd76bb2 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 325 | std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> |
| 326 | MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin, |
| 327 | MachineInstr::mmo_iterator End) { |
| 328 | // Count the number of load mem refs. |
| 329 | unsigned Num = 0; |
| 330 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) |
| 331 | if ((*I)->isLoad()) |
| 332 | ++Num; |
| 333 | |
| 334 | // Allocate a new array and populate it with the load information. |
| 335 | MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); |
| 336 | unsigned Index = 0; |
| 337 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { |
| 338 | if ((*I)->isLoad()) { |
| 339 | if (!(*I)->isStore()) |
| 340 | // Reuse the MMO. |
| 341 | Result[Index] = *I; |
| 342 | else { |
| 343 | // Clone the MMO and unset the store flag. |
| 344 | MachineMemOperand *JustLoad = |
Chris Lattner | b5f4920 | 2010-09-21 04:46:39 +0000 | [diff] [blame] | 345 | getMachineMemOperand((*I)->getPointerInfo(), |
Dan Gohman | dd76bb2 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 346 | (*I)->getFlags() & ~MachineMemOperand::MOStore, |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 347 | (*I)->getSize(), (*I)->getBaseAlignment(), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 348 | (*I)->getAAInfo()); |
Dan Gohman | dd76bb2 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 349 | Result[Index] = JustLoad; |
| 350 | } |
| 351 | ++Index; |
| 352 | } |
| 353 | } |
| 354 | return std::make_pair(Result, Result + Num); |
| 355 | } |
| 356 | |
| 357 | std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> |
| 358 | MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin, |
| 359 | MachineInstr::mmo_iterator End) { |
| 360 | // Count the number of load mem refs. |
| 361 | unsigned Num = 0; |
| 362 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) |
| 363 | if ((*I)->isStore()) |
| 364 | ++Num; |
| 365 | |
| 366 | // Allocate a new array and populate it with the store information. |
| 367 | MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); |
| 368 | unsigned Index = 0; |
| 369 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { |
| 370 | if ((*I)->isStore()) { |
| 371 | if (!(*I)->isLoad()) |
| 372 | // Reuse the MMO. |
| 373 | Result[Index] = *I; |
| 374 | else { |
| 375 | // Clone the MMO and unset the load flag. |
| 376 | MachineMemOperand *JustStore = |
Chris Lattner | b5f4920 | 2010-09-21 04:46:39 +0000 | [diff] [blame] | 377 | getMachineMemOperand((*I)->getPointerInfo(), |
Dan Gohman | dd76bb2 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 378 | (*I)->getFlags() & ~MachineMemOperand::MOLoad, |
Dan Gohman | a94cc6d | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 379 | (*I)->getSize(), (*I)->getBaseAlignment(), |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 380 | (*I)->getAAInfo()); |
Dan Gohman | dd76bb2 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 381 | Result[Index] = JustStore; |
| 382 | } |
| 383 | ++Index; |
| 384 | } |
| 385 | } |
| 386 | return std::make_pair(Result, Result + Num); |
| 387 | } |
| 388 | |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 389 | const char *MachineFunction::createExternalSymbolName(StringRef Name) { |
| 390 | char *Dest = Allocator.Allocate<char>(Name.size() + 1); |
| 391 | std::copy(Name.begin(), Name.end(), Dest); |
| 392 | Dest[Name.size()] = 0; |
| 393 | return Dest; |
| 394 | } |
| 395 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 396 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 397 | LLVM_DUMP_METHOD void MachineFunction::dump() const { |
David Greene | 6671011 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 398 | print(dbgs()); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 399 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 400 | #endif |
Chris Lattner | 214808f | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 401 | |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 402 | StringRef MachineFunction::getName() const { |
| 403 | assert(getFunction() && "No function!"); |
| 404 | return getFunction()->getName(); |
| 405 | } |
| 406 | |
Matthias Braun | 0e881d6 | 2016-05-05 18:14:43 +0000 | [diff] [blame] | 407 | void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 408 | OS << "# Machine code for function " << getName() << ": "; |
Derek Schuff | 07636cd | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 409 | OS << "Properties: <"; |
| 410 | getProperties().print(OS); |
Derek Schuff | f7b2bce | 2016-04-11 23:32:13 +0000 | [diff] [blame] | 411 | OS << ">\n"; |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 412 | |
| 413 | // Print Frame Information |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 414 | FrameInfo->print(*this, OS); |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 415 | |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 416 | // Print JumpTable Information |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 417 | if (JumpTableInfo) |
| 418 | JumpTableInfo->print(OS); |
Chris Lattner | c6807e8 | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 419 | |
| 420 | // Print Constant Pool |
Chris Lattner | 22d4bfc | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 421 | ConstantPool->print(OS); |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 422 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 423 | const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 424 | |
Matthijs Kooijman | c8d7988 | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 425 | if (RegInfo && !RegInfo->livein_empty()) { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 426 | OS << "Function Live Ins: "; |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 427 | for (MachineRegisterInfo::livein_iterator |
| 428 | I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 6297a71 | 2011-05-02 20:06:30 +0000 | [diff] [blame] | 429 | OS << PrintReg(I->first, TRI); |
Chris Lattner | 52d0c78 | 2006-05-16 05:55:30 +0000 | [diff] [blame] | 430 | if (I->second) |
Jakob Stoklund Olesen | 6297a71 | 2011-05-02 20:06:30 +0000 | [diff] [blame] | 431 | OS << " in " << PrintReg(I->second, TRI); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 432 | if (std::next(I) != E) |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 433 | OS << ", "; |
Chris Lattner | d4d10ff | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 434 | } |
Chris Lattner | 22d4bfc | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 435 | OS << '\n'; |
Chris Lattner | d4d10ff | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 436 | } |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 437 | |
Duncan P. N. Exon Smith | 32692154 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 438 | ModuleSlotTracker MST(getFunction()->getParent()); |
| 439 | MST.incorporateFunction(*getFunction()); |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 440 | for (const auto &BB : *this) { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 441 | OS << '\n'; |
Duncan P. N. Exon Smith | 32692154 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 442 | BB.print(OS, MST, Indexes); |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 443 | } |
Brian Gaeke | 2fe0ac9 | 2004-03-29 21:58:31 +0000 | [diff] [blame] | 444 | |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 445 | OS << "\n# End machine code for function " << getName() << ".\n\n"; |
Chris Lattner | 214808f | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Alkis Evlogimenos | 2c422bb | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 448 | namespace llvm { |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 449 | template<> |
| 450 | struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { |
Tobias Grosser | 90d3340 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 451 | |
| 452 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 453 | |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 454 | static std::string getGraphName(const MachineFunction *F) { |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 455 | return ("CFG for '" + F->getName() + "' function").str(); |
Alkis Evlogimenos | 2c422bb | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Tobias Grosser | dd7f2e7 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 458 | std::string getNodeLabel(const MachineBasicBlock *Node, |
| 459 | const MachineFunction *Graph) { |
Chris Lattner | 565449d | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 460 | std::string OutStr; |
| 461 | { |
| 462 | raw_string_ostream OSS(OutStr); |
Jakob Stoklund Olesen | 80717dd | 2010-10-30 01:26:19 +0000 | [diff] [blame] | 463 | |
| 464 | if (isSimple()) { |
| 465 | OSS << "BB#" << Node->getNumber(); |
| 466 | if (const BasicBlock *BB = Node->getBasicBlock()) |
| 467 | OSS << ": " << BB->getName(); |
| 468 | } else |
Chris Lattner | 565449d | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 469 | Node->print(OSS); |
Alkis Evlogimenos | 2c422bb | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 470 | } |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 471 | |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 472 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 473 | |
| 474 | // Process string output to make it nicer... |
| 475 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 476 | if (OutStr[i] == '\n') { // Left justify |
| 477 | OutStr[i] = '\\'; |
| 478 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 479 | } |
| 480 | return OutStr; |
| 481 | } |
| 482 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 483 | } |
Alkis Evlogimenos | 2c422bb | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 484 | |
| 485 | void MachineFunction::viewCFG() const |
| 486 | { |
Jim Laskey | d00db25 | 2005-10-12 12:09:05 +0000 | [diff] [blame] | 487 | #ifndef NDEBUG |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 488 | ViewGraph(this, "mf" + getName()); |
Reid Spencer | ee7eaa2 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 489 | #else |
Dan Gohman | 7692014 | 2010-07-07 17:28:45 +0000 | [diff] [blame] | 490 | errs() << "MachineFunction::viewCFG is only available in debug builds on " |
Daniel Dunbar | 34ee203 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 491 | << "systems with Graphviz or gv!\n"; |
Reid Spencer | ee7eaa2 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 492 | #endif // NDEBUG |
Alkis Evlogimenos | 2c422bb | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void MachineFunction::viewCFGOnly() const |
| 496 | { |
Owen Anderson | b70adf2 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 497 | #ifndef NDEBUG |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 498 | ViewGraph(this, "mf" + getName(), true); |
Owen Anderson | b70adf2 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 499 | #else |
Dan Gohman | 7692014 | 2010-07-07 17:28:45 +0000 | [diff] [blame] | 500 | errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " |
Daniel Dunbar | 34ee203 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 501 | << "systems with Graphviz or gv!\n"; |
Owen Anderson | b70adf2 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 502 | #endif // NDEBUG |
Alkis Evlogimenos | 2c422bb | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 505 | /// Add the specified physical register as a live-in value and |
Bob Wilson | f8b8547 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 506 | /// create a corresponding virtual register for it. |
| 507 | unsigned MachineFunction::addLiveIn(unsigned PReg, |
Devang Patel | f3292b2 | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 508 | const TargetRegisterClass *RC) { |
Evan Cheng | 1b79bab | 2010-05-24 21:33:37 +0000 | [diff] [blame] | 509 | MachineRegisterInfo &MRI = getRegInfo(); |
| 510 | unsigned VReg = MRI.getLiveInVirtReg(PReg); |
| 511 | if (VReg) { |
Quentin Colombet | 18b779e | 2013-12-12 00:15:47 +0000 | [diff] [blame] | 512 | const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); |
| 513 | (void)VRegRC; |
| 514 | // A physical register can be added several times. |
| 515 | // Between two calls, the register class of the related virtual register |
| 516 | // may have been constrained to match some operation constraints. |
| 517 | // In that case, check that the current register class includes the |
| 518 | // physical register and is a sub class of the specified RC. |
| 519 | assert((VRegRC == RC || (VRegRC->contains(PReg) && |
| 520 | RC->hasSubClassEq(VRegRC))) && |
| 521 | "Register class mismatch!"); |
Evan Cheng | 1b79bab | 2010-05-24 21:33:37 +0000 | [diff] [blame] | 522 | return VReg; |
| 523 | } |
| 524 | VReg = MRI.createVirtualRegister(RC); |
| 525 | MRI.addLiveIn(PReg, VReg); |
Bob Wilson | f8b8547 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 526 | return VReg; |
| 527 | } |
| 528 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 529 | /// Return the MCSymbol for the specified non-empty jump table. |
Bill Wendling | 3632171 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 530 | /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a |
| 531 | /// normal 'L' label is returned. |
NAKAMURA Takumi | c403be1 | 2014-06-25 12:40:56 +0000 | [diff] [blame] | 532 | MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, |
Bill Wendling | 3632171 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 533 | bool isLinkerPrivate) const { |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 534 | const DataLayout &DL = getDataLayout(); |
Chris Lattner | 8a785d7 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 535 | assert(JumpTableInfo && "No jump tables"); |
Chandler Carruth | c07bd40 | 2010-01-27 10:27:10 +0000 | [diff] [blame] | 536 | assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 537 | |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 538 | const char *Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() |
| 539 | : DL.getPrivateGlobalPrefix(); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 540 | SmallString<60> Name; |
| 541 | raw_svector_ostream(Name) |
| 542 | << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 543 | return Ctx.getOrCreateSymbol(Name); |
Chris Lattner | 8a785d7 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 546 | /// Return a function-local symbol to represent the PIC base. |
Chris Lattner | 7077efe | 2010-11-14 22:48:15 +0000 | [diff] [blame] | 547 | MCSymbol *MachineFunction::getPICBaseSymbol() const { |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 548 | const DataLayout &DL = getDataLayout(); |
| 549 | return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + |
| 550 | Twine(getFunctionNumber()) + "$pb"); |
Chris Lattner | 7077efe | 2010-11-14 22:48:15 +0000 | [diff] [blame] | 551 | } |
Chris Lattner | 8a785d7 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 553 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca4362f | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 554 | // MachineFrameInfo implementation |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 555 | //===----------------------------------------------------------------------===// |
| 556 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 557 | /// Make sure the function is at least Align bytes aligned. |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 558 | void MachineFrameInfo::ensureMaxAlignment(unsigned Align) { |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 559 | if (!StackRealignable) |
Eric Christopher | 000ef03 | 2014-10-08 08:46:34 +0000 | [diff] [blame] | 560 | assert(Align <= StackAlignment && |
Manman Ren | f563941 | 2012-12-04 00:52:33 +0000 | [diff] [blame] | 561 | "For targets without stack realignment, Align is out of limit!"); |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 562 | if (MaxAlignment < Align) MaxAlignment = Align; |
| 563 | } |
| 564 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 565 | /// Clamp the alignment if requested and emit a warning. |
Bob Wilson | 67bbf3a | 2013-02-08 20:35:15 +0000 | [diff] [blame] | 566 | static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align, |
| 567 | unsigned StackAlign) { |
| 568 | if (!ShouldClamp || Align <= StackAlign) |
| 569 | return Align; |
| 570 | DEBUG(dbgs() << "Warning: requested alignment " << Align |
| 571 | << " exceeds the stack alignment " << StackAlign |
| 572 | << " when stack realignment is off" << '\n'); |
Manman Ren | f563941 | 2012-12-04 00:52:33 +0000 | [diff] [blame] | 573 | return StackAlign; |
| 574 | } |
| 575 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 576 | /// Create a new statically sized stack object, returning a nonnegative |
| 577 | /// identifier to represent it. |
Bob Wilson | 67bbf3a | 2013-02-08 20:35:15 +0000 | [diff] [blame] | 578 | int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment, |
Josh Magee | 22b8ba2 | 2013-12-19 03:17:11 +0000 | [diff] [blame] | 579 | bool isSS, const AllocaInst *Alloca) { |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 580 | assert(Size != 0 && "Cannot allocate zero size stack objects!"); |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 581 | Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment); |
Hal Finkel | 0815a05 | 2014-08-16 00:17:02 +0000 | [diff] [blame] | 582 | Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca, |
| 583 | !isSS)); |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 584 | int Index = (int)Objects.size() - NumFixedObjects - 1; |
| 585 | assert(Index >= 0 && "Bad frame index!"); |
| 586 | ensureMaxAlignment(Alignment); |
| 587 | return Index; |
| 588 | } |
| 589 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 590 | /// Create a new statically sized stack object that represents a spill slot, |
| 591 | /// returning a nonnegative identifier to represent it. |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 592 | int MachineFrameInfo::CreateSpillStackObject(uint64_t Size, |
| 593 | unsigned Alignment) { |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 594 | Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment); |
Josh Magee | 22b8ba2 | 2013-12-19 03:17:11 +0000 | [diff] [blame] | 595 | CreateStackObject(Size, Alignment, true); |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 596 | int Index = (int)Objects.size() - NumFixedObjects - 1; |
| 597 | ensureMaxAlignment(Alignment); |
| 598 | return Index; |
| 599 | } |
| 600 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 601 | /// Notify the MachineFrameInfo object that a variable sized object has been |
| 602 | /// created. This must be created whenever a variable sized object is created, |
| 603 | /// whether or not the index returned is actually used. |
Josh Magee | 22b8ba2 | 2013-12-19 03:17:11 +0000 | [diff] [blame] | 604 | int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment, |
| 605 | const AllocaInst *Alloca) { |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 606 | HasVarSizedObjects = true; |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 607 | Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment); |
Hal Finkel | 0815a05 | 2014-08-16 00:17:02 +0000 | [diff] [blame] | 608 | Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true)); |
Manman Ren | 26c73f9 | 2012-12-04 00:26:44 +0000 | [diff] [blame] | 609 | ensureMaxAlignment(Alignment); |
| 610 | return (int)Objects.size()-NumFixedObjects-1; |
| 611 | } |
| 612 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 613 | /// Create a new object at a fixed location on the stack. |
Chris Lattner | 6068832 | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 614 | /// All fixed objects should be created before other objects are created for |
| 615 | /// efficiency. By default, fixed objects are immutable. This returns an |
| 616 | /// index with a negative value. |
Chris Lattner | 6068832 | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 617 | int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, |
Hal Finkel | 0815a05 | 2014-08-16 00:17:02 +0000 | [diff] [blame] | 618 | bool Immutable, bool isAliased) { |
Chris Lattner | 6068832 | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 619 | assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); |
Evan Cheng | f3aeb2c | 2010-07-04 18:52:05 +0000 | [diff] [blame] | 620 | // The alignment of the frame index can be determined from its offset from |
| 621 | // the incoming frame position. If the frame object is at offset 32 and |
| 622 | // the stack is guaranteed to be 16-byte aligned, then we know that the |
Charles Davis | 2f65f35 | 2016-04-09 23:34:42 +0000 | [diff] [blame] | 623 | // object is 16-byte aligned. Note that unlike the non-fixed case, if the |
| 624 | // stack needs realignment, we can't assume that the stack will in fact be |
| 625 | // aligned. |
| 626 | unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment); |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 627 | Align = clampStackAlignment(!StackRealignable, Align, StackAlignment); |
Evan Cheng | f3aeb2c | 2010-07-04 18:52:05 +0000 | [diff] [blame] | 628 | Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable, |
Nadav Rotem | 7c277da | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 629 | /*isSS*/ false, |
Hal Finkel | 0815a05 | 2014-08-16 00:17:02 +0000 | [diff] [blame] | 630 | /*Alloca*/ nullptr, isAliased)); |
Chris Lattner | 6068832 | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 631 | return -++NumFixedObjects; |
| 632 | } |
| 633 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 634 | /// Create a spill slot at a fixed location on the stack. |
| 635 | /// Returns an index with a negative value. |
NAKAMURA Takumi | 1db5995 | 2014-06-25 12:41:52 +0000 | [diff] [blame] | 636 | int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size, |
| 637 | int64_t SPOffset) { |
Charles Davis | 2f65f35 | 2016-04-09 23:34:42 +0000 | [diff] [blame] | 638 | unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment); |
Reid Kleckner | b6800b3 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 639 | Align = clampStackAlignment(!StackRealignable, Align, StackAlignment); |
NAKAMURA Takumi | 1db5995 | 2014-06-25 12:41:52 +0000 | [diff] [blame] | 640 | Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, |
| 641 | /*Immutable*/ true, |
| 642 | /*isSS*/ true, |
Hal Finkel | 0815a05 | 2014-08-16 00:17:02 +0000 | [diff] [blame] | 643 | /*Alloca*/ nullptr, |
| 644 | /*isAliased*/ false)); |
NAKAMURA Takumi | 1db5995 | 2014-06-25 12:41:52 +0000 | [diff] [blame] | 645 | return -++NumFixedObjects; |
| 646 | } |
Chris Lattner | 6068832 | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 647 | |
Matthias Braun | 111f5d8 | 2015-05-28 23:20:35 +0000 | [diff] [blame] | 648 | BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const { |
| 649 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Jakob Stoklund Olesen | 3de4a60 | 2009-08-13 16:19:33 +0000 | [diff] [blame] | 650 | BitVector BV(TRI->getNumRegs()); |
| 651 | |
| 652 | // Before CSI is calculated, no registers are considered pristine. They can be |
| 653 | // freely used and PEI will make sure they are saved. |
| 654 | if (!isCalleeSavedInfoValid()) |
| 655 | return BV; |
| 656 | |
Matthias Braun | 111f5d8 | 2015-05-28 23:20:35 +0000 | [diff] [blame] | 657 | for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR) |
Jakob Stoklund Olesen | 3de4a60 | 2009-08-13 16:19:33 +0000 | [diff] [blame] | 658 | BV.set(*CSR); |
| 659 | |
Matthias Braun | 111f5d8 | 2015-05-28 23:20:35 +0000 | [diff] [blame] | 660 | // Saved CSRs are not pristine. |
Krzysztof Parzyszek | df537b9 | 2015-11-19 21:18:52 +0000 | [diff] [blame] | 661 | for (auto &I : getCalleeSavedInfo()) |
| 662 | for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S) |
| 663 | BV.reset(*S); |
Jakob Stoklund Olesen | 3de4a60 | 2009-08-13 16:19:33 +0000 | [diff] [blame] | 664 | |
| 665 | return BV; |
| 666 | } |
| 667 | |
Hal Finkel | 628ba12 | 2013-03-14 21:15:20 +0000 | [diff] [blame] | 668 | unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 669 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
| 670 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
Hal Finkel | 628ba12 | 2013-03-14 21:15:20 +0000 | [diff] [blame] | 671 | unsigned MaxAlign = getMaxAlignment(); |
| 672 | int Offset = 0; |
| 673 | |
| 674 | // This code is very, very similar to PEI::calculateFrameObjectOffsets(). |
| 675 | // It really should be refactored to share code. Until then, changes |
| 676 | // should keep in mind that there's tight coupling between the two. |
| 677 | |
| 678 | for (int i = getObjectIndexBegin(); i != 0; ++i) { |
| 679 | int FixedOff = -getObjectOffset(i); |
| 680 | if (FixedOff > Offset) Offset = FixedOff; |
| 681 | } |
| 682 | for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) { |
| 683 | if (isDeadObjectIndex(i)) |
| 684 | continue; |
| 685 | Offset += getObjectSize(i); |
| 686 | unsigned Align = getObjectAlignment(i); |
| 687 | // Adjust to alignment boundary |
| 688 | Offset = (Offset+Align-1)/Align*Align; |
| 689 | |
| 690 | MaxAlign = std::max(Align, MaxAlign); |
| 691 | } |
| 692 | |
| 693 | if (adjustsStack() && TFI->hasReservedCallFrame(MF)) |
| 694 | Offset += getMaxCallFrameSize(); |
| 695 | |
| 696 | // Round up the size to a multiple of the alignment. If the function has |
| 697 | // any calls or alloca's, align to the target's StackAlignment value to |
| 698 | // ensure that the callee's frame or the alloca data is suitably aligned; |
| 699 | // otherwise, for leaf functions, align to the TransientStackAlignment |
| 700 | // value. |
| 701 | unsigned StackAlign; |
| 702 | if (adjustsStack() || hasVarSizedObjects() || |
| 703 | (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0)) |
| 704 | StackAlign = TFI->getStackAlignment(); |
| 705 | else |
| 706 | StackAlign = TFI->getTransientStackAlignment(); |
| 707 | |
| 708 | // If the frame pointer is eliminated, all frame offsets will be relative to |
| 709 | // SP not FP. Align to MaxAlign so this works. |
| 710 | StackAlign = std::max(StackAlign, MaxAlign); |
| 711 | unsigned AlignMask = StackAlign - 1; |
| 712 | Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); |
| 713 | |
| 714 | return (unsigned)Offset; |
| 715 | } |
Jakob Stoklund Olesen | 3de4a60 | 2009-08-13 16:19:33 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 22d4bfc | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 717 | void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{ |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 718 | if (Objects.empty()) return; |
| 719 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 720 | const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering(); |
Matthijs Kooijman | 2530f5f | 2008-11-03 11:16:43 +0000 | [diff] [blame] | 721 | int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); |
Chris Lattner | eb45c98 | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 722 | |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 723 | OS << "Frame Objects:\n"; |
| 724 | |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 725 | for (unsigned i = 0, e = Objects.size(); i != e; ++i) { |
| 726 | const StackObject &SO = Objects[i]; |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 727 | OS << " fi#" << (int)(i-NumFixedObjects) << ": "; |
Evan Cheng | 6d56368 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 728 | if (SO.Size == ~0ULL) { |
| 729 | OS << "dead\n"; |
| 730 | continue; |
| 731 | } |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 732 | if (SO.Size == 0) |
| 733 | OS << "variable sized"; |
| 734 | else |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 735 | OS << "size=" << SO.Size; |
| 736 | OS << ", align=" << SO.Alignment; |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 738 | if (i < NumFixedObjects) |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 739 | OS << ", fixed"; |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 740 | if (i < NumFixedObjects || SO.SPOffset != -1) { |
Chris Lattner | 9bd98ea | 2007-04-25 04:20:54 +0000 | [diff] [blame] | 741 | int64_t Off = SO.SPOffset - ValOffset; |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 742 | OS << ", at location [SP"; |
Chris Lattner | eb45c98 | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 743 | if (Off > 0) |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 744 | OS << "+" << Off; |
Chris Lattner | eb45c98 | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 745 | else if (Off < 0) |
Alkis Evlogimenos | 58350a7 | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 746 | OS << Off; |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 747 | OS << "]"; |
| 748 | } |
| 749 | OS << "\n"; |
| 750 | } |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 753 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chris Lattner | eb45c98 | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 754 | void MachineFrameInfo::dump(const MachineFunction &MF) const { |
David Greene | 6671011 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 755 | print(MF, dbgs()); |
Chris Lattner | eb45c98 | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 756 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 757 | #endif |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 3252564 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 759 | //===----------------------------------------------------------------------===// |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 760 | // MachineJumpTableInfo implementation |
| 761 | //===----------------------------------------------------------------------===// |
| 762 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 763 | /// Return the size of each entry in the jump table. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 764 | unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 765 | // The size of a jump table entry is 4 bytes unless the entry is just the |
| 766 | // address of a block, in which case it is the pointer size. |
| 767 | switch (getEntryKind()) { |
| 768 | case MachineJumpTableInfo::EK_BlockAddress: |
Chandler Carruth | 5da3f05 | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 769 | return TD.getPointerSize(); |
Akira Hatanaka | f0b0844 | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 770 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
| 771 | return 8; |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 772 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 773 | case MachineJumpTableInfo::EK_LabelDifference32: |
Chris Lattner | 5fc4160 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 774 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 775 | return 4; |
Richard Osborne | 6d3e92d | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 776 | case MachineJumpTableInfo::EK_Inline: |
| 777 | return 0; |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 778 | } |
Craig Topper | accd351 | 2012-02-06 08:17:43 +0000 | [diff] [blame] | 779 | llvm_unreachable("Unknown jump table encoding!"); |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 782 | /// Return the alignment of each entry in the jump table. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 783 | unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 784 | // The alignment of a jump table entry is the alignment of int32 unless the |
| 785 | // entry is just the address of a block, in which case it is the pointer |
| 786 | // alignment. |
| 787 | switch (getEntryKind()) { |
| 788 | case MachineJumpTableInfo::EK_BlockAddress: |
Chandler Carruth | 5da3f05 | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 789 | return TD.getPointerABIAlignment(); |
Akira Hatanaka | f0b0844 | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 790 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
| 791 | return TD.getABIIntegerTypeAlignment(64); |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 792 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 793 | case MachineJumpTableInfo::EK_LabelDifference32: |
Chris Lattner | 5fc4160 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 794 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 795 | return TD.getABIIntegerTypeAlignment(32); |
Richard Osborne | 6d3e92d | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 796 | case MachineJumpTableInfo::EK_Inline: |
| 797 | return 1; |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 798 | } |
Craig Topper | accd351 | 2012-02-06 08:17:43 +0000 | [diff] [blame] | 799 | llvm_unreachable("Unknown jump table encoding!"); |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 802 | /// Create a new jump table entry in the jump table info. |
Bob Wilson | 3c7cde4 | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 803 | unsigned MachineJumpTableInfo::createJumpTableIndex( |
Chris Lattner | cde339c | 2006-10-28 18:17:09 +0000 | [diff] [blame] | 804 | const std::vector<MachineBasicBlock*> &DestBBs) { |
Chris Lattner | 28328f9 | 2006-10-28 18:11:20 +0000 | [diff] [blame] | 805 | assert(!DestBBs.empty() && "Cannot create an empty jump table!"); |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 806 | JumpTables.push_back(MachineJumpTableEntry(DestBBs)); |
| 807 | return JumpTables.size()-1; |
| 808 | } |
| 809 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 810 | /// If Old is the target of any jump tables, update the jump tables to branch |
| 811 | /// to New instead. |
Chris Lattner | 273735b | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 812 | bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, |
| 813 | MachineBasicBlock *New) { |
Dan Gohman | 505065c | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 814 | assert(Old != New && "Not making a change?"); |
| 815 | bool MadeChange = false; |
Jim Grosbach | 9c8609e | 2009-11-14 20:09:13 +0000 | [diff] [blame] | 816 | for (size_t i = 0, e = JumpTables.size(); i != e; ++i) |
| 817 | ReplaceMBBInJumpTable(i, Old, New); |
| 818 | return MadeChange; |
| 819 | } |
| 820 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 821 | /// If Old is a target of the jump tables, update the jump table to branch to |
| 822 | /// New instead. |
Chris Lattner | 273735b | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 823 | bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, |
| 824 | MachineBasicBlock *Old, |
| 825 | MachineBasicBlock *New) { |
Jim Grosbach | 9c8609e | 2009-11-14 20:09:13 +0000 | [diff] [blame] | 826 | assert(Old != New && "Not making a change?"); |
| 827 | bool MadeChange = false; |
| 828 | MachineJumpTableEntry &JTE = JumpTables[Idx]; |
| 829 | for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) |
| 830 | if (JTE.MBBs[j] == Old) { |
| 831 | JTE.MBBs[j] = New; |
| 832 | MadeChange = true; |
| 833 | } |
Dan Gohman | 505065c | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 834 | return MadeChange; |
| 835 | } |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 836 | |
Chris Lattner | 22d4bfc | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 837 | void MachineJumpTableInfo::print(raw_ostream &OS) const { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 838 | if (JumpTables.empty()) return; |
| 839 | |
| 840 | OS << "Jump Tables:\n"; |
| 841 | |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 842 | for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 843 | OS << " jt#" << i << ": "; |
| 844 | for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) |
| 845 | OS << " BB#" << JumpTables[i].MBBs[j]->getNumber(); |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 846 | } |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 847 | |
| 848 | OS << '\n'; |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 851 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 852 | LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 853 | #endif |
Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 854 | |
| 855 | |
| 856 | //===----------------------------------------------------------------------===// |
Chris Lattner | c6807e8 | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 857 | // MachineConstantPool implementation |
| 858 | //===----------------------------------------------------------------------===// |
| 859 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 860 | void MachineConstantPoolValue::anchor() { } |
| 861 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 862 | Type *MachineConstantPoolEntry::getType() const { |
Evan Cheng | 4f929955 | 2006-09-14 05:50:57 +0000 | [diff] [blame] | 863 | if (isMachineConstantPoolEntry()) |
Chris Lattner | cfb01e2 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 864 | return Val.MachineCPVal->getType(); |
Evan Cheng | 4f929955 | 2006-09-14 05:50:57 +0000 | [diff] [blame] | 865 | return Val.ConstVal->getType(); |
| 866 | } |
| 867 | |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 868 | bool MachineConstantPoolEntry::needsRelocation() const { |
Chris Lattner | cfb01e2 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 869 | if (isMachineConstantPoolEntry()) |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 870 | return true; |
| 871 | return Val.ConstVal->needsRelocation(); |
Chris Lattner | cfb01e2 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 872 | } |
| 873 | |
David Majnemer | 5a1c4b8 | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 874 | SectionKind |
| 875 | MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 876 | if (needsRelocation()) |
| 877 | return SectionKind::getReadOnlyWithRel(); |
| 878 | switch (DL->getTypeAllocSize(getType())) { |
| 879 | case 4: |
| 880 | return SectionKind::getMergeableConst4(); |
| 881 | case 8: |
| 882 | return SectionKind::getMergeableConst8(); |
| 883 | case 16: |
| 884 | return SectionKind::getMergeableConst16(); |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 885 | case 32: |
| 886 | return SectionKind::getMergeableConst32(); |
David Majnemer | 5a1c4b8 | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 887 | default: |
Rafael Espindola | 65e4902 | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 888 | return SectionKind::getReadOnly(); |
David Majnemer | 5a1c4b8 | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 889 | } |
David Majnemer | 5a1c4b8 | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 892 | MachineConstantPool::~MachineConstantPool() { |
| 893 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
| 894 | if (Constants[i].isMachineConstantPoolEntry()) |
| 895 | delete Constants[i].Val.MachineCPVal; |
Cameron Zwarich | 7cf8876 | 2011-02-22 08:54:30 +0000 | [diff] [blame] | 896 | for (DenseSet<MachineConstantPoolValue*>::iterator I = |
| 897 | MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); |
| 898 | I != E; ++I) |
| 899 | delete *I; |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 902 | /// Test whether the given two constants can be allocated the same constant pool |
| 903 | /// entry. |
Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 904 | static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 905 | const DataLayout &DL) { |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 906 | // Handle the trivial case quickly. |
| 907 | if (A == B) return true; |
| 908 | |
| 909 | // If they have the same type but weren't the same constant, quickly |
| 910 | // reject them. |
| 911 | if (A->getType() == B->getType()) return false; |
| 912 | |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 913 | // We can't handle structs or arrays. |
| 914 | if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || |
| 915 | isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) |
| 916 | return false; |
NAKAMURA Takumi | c403be1 | 2014-06-25 12:40:56 +0000 | [diff] [blame] | 917 | |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 918 | // For now, only support constants with the same size. |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 919 | uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); |
| 920 | if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 921 | return false; |
| 922 | |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 923 | Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 925 | // Try constant folding a bitcast of both instructions to an integer. If we |
| 926 | // get two identical ConstantInt's, then we are good to share them. We use |
| 927 | // the constant folding APIs to do this so that we get the benefit of |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 928 | // DataLayout. |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 929 | if (isa<PointerType>(A->getType())) |
Manuel Jacob | 925d029 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 930 | A = ConstantFoldCastOperand(Instruction::PtrToInt, |
| 931 | const_cast<Constant *>(A), IntTy, DL); |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 932 | else if (A->getType() != IntTy) |
Manuel Jacob | 925d029 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 933 | A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), |
| 934 | IntTy, DL); |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 935 | if (isa<PointerType>(B->getType())) |
Manuel Jacob | 925d029 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 936 | B = ConstantFoldCastOperand(Instruction::PtrToInt, |
| 937 | const_cast<Constant *>(B), IntTy, DL); |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 938 | else if (B->getType() != IntTy) |
Manuel Jacob | 925d029 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 939 | B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), |
| 940 | IntTy, DL); |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 7ba8183 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 942 | return A == B; |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Sanjay Patel | 41044f8 | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 945 | /// Create a new entry in the constant pool or return an existing one. |
| 946 | /// User must specify the log2 of the minimum required alignment for the object. |
NAKAMURA Takumi | c403be1 | 2014-06-25 12:40:56 +0000 | [diff] [blame] | 947 | unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, |
Chris Lattner | f619082 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 948 | unsigned Alignment) { |
| 949 | assert(Alignment && "Alignment must be specified!"); |
| 950 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 951 | |
Chris Lattner | f619082 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 952 | // Check to see if we already have this constant. |
| 953 | // |
| 954 | // FIXME, this could be made much more efficient for large constant pools. |
Chris Lattner | f619082 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 955 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 956 | if (!Constants[i].isMachineConstantPoolEntry() && |
Mehdi Amini | 42e9f96 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 957 | CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 958 | if ((unsigned)Constants[i].getAlignment() < Alignment) |
| 959 | Constants[i].Alignment = Alignment; |
Chris Lattner | f619082 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 960 | return i; |
Dan Gohman | 75d6a4a | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 961 | } |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 962 | |
Evan Cheng | 1fb8aed | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 963 | Constants.push_back(MachineConstantPoolEntry(C, Alignment)); |
Chris Lattner | f619082 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 964 | return Constants.size()-1; |
| 965 | } |
| 966 | |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 967 | unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, |
| 968 | unsigned Alignment) { |
| 969 | assert(Alignment && "Alignment must be specified!"); |
| 970 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
Chad Rosier | 651f9a4 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 971 | |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 972 | // Check to see if we already have this constant. |
| 973 | // |
| 974 | // FIXME, this could be made much more efficient for large constant pools. |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 975 | int Idx = V->getExistingMachineCPValue(this, Alignment); |
Cameron Zwarich | 7cf8876 | 2011-02-22 08:54:30 +0000 | [diff] [blame] | 976 | if (Idx != -1) { |
| 977 | MachineCPVsSharingEntries.insert(V); |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 978 | return (unsigned)Idx; |
Cameron Zwarich | 7cf8876 | 2011-02-22 08:54:30 +0000 | [diff] [blame] | 979 | } |
Evan Cheng | 1fb8aed | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 980 | |
| 981 | Constants.push_back(MachineConstantPoolEntry(V, Alignment)); |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 982 | return Constants.size()-1; |
| 983 | } |
| 984 | |
Chris Lattner | 838aff3 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 985 | void MachineConstantPool::print(raw_ostream &OS) const { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 986 | if (Constants.empty()) return; |
| 987 | |
| 988 | OS << "Constant Pool:\n"; |
Evan Cheng | 32be2dc | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 989 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 990 | OS << " cp#" << i << ": "; |
Evan Cheng | 45fe3bc | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 991 | if (Constants[i].isMachineConstantPoolEntry()) |
| 992 | Constants[i].Val.MachineCPVal->print(OS); |
| 993 | else |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 994 | Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 995 | OS << ", align=" << Constants[i].getAlignment(); |
Evan Cheng | 32be2dc | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 996 | OS << "\n"; |
| 997 | } |
Chris Lattner | c6807e8 | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 1000 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1001 | LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 1002 | #endif |