Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 1 | //===- MachineFunction.cpp ------------------------------------------------===// |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 6b94453 | 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 | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 15 | |
David Greene | 098612b | 2009-08-19 22:16:11 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/BitVector.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
David Majnemer | 1114aa2 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/EHPersonalities.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 6b27607 | 2012-01-27 01:47:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 30 | #include "llvm/CodeGen/MachineInstr.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineMemOperand.h" |
Chris Lattner | 820e55e | 2010-04-05 05:49:50 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | de0129a | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/PseudoSourceValue.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 37 | #include "llvm/CodeGen/TargetLowering.h" |
| 38 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 39 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Heejin Ahn | c430c83 | 2018-06-19 00:26:39 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/WasmEHFuncInfo.h" |
Reid Kleckner | 595419d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 42 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Attributes.h" |
| 44 | #include "llvm/IR/BasicBlock.h" |
| 45 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 46 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 47 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 48 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 49 | #include "llvm/IR/GlobalValue.h" |
| 50 | #include "llvm/IR/Instruction.h" |
| 51 | #include "llvm/IR/Instructions.h" |
| 52 | #include "llvm/IR/Metadata.h" |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Module.h" |
Duncan P. N. Exon Smith | 9a61a42 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 54 | #include "llvm/IR/ModuleSlotTracker.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 55 | #include "llvm/IR/Value.h" |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 56 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 57 | #include "llvm/MC/MCSymbol.h" |
| 58 | #include "llvm/MC/SectionKind.h" |
| 59 | #include "llvm/Support/Casting.h" |
| 60 | #include "llvm/Support/CommandLine.h" |
| 61 | #include "llvm/Support/Compiler.h" |
| 62 | #include "llvm/Support/DOTGraphTraits.h" |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 64 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | f28bbda | 2006-10-03 20:19:23 +0000 | [diff] [blame] | 65 | #include "llvm/Support/GraphWriter.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 66 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 67 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 68 | #include <algorithm> |
| 69 | #include <cassert> |
| 70 | #include <cstddef> |
| 71 | #include <cstdint> |
| 72 | #include <iterator> |
| 73 | #include <string> |
| 74 | #include <utility> |
| 75 | #include <vector> |
| 76 | |
Chris Lattner | 07f32d4 | 2003-12-20 09:17:07 +0000 | [diff] [blame] | 77 | using namespace llvm; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 78 | |
Chandler Carruth | 283b399 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 79 | #define DEBUG_TYPE "codegen" |
| 80 | |
Chad Rosier | 8b2e79c | 2015-12-29 18:18:07 +0000 | [diff] [blame] | 81 | static cl::opt<unsigned> |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 82 | AlignAllFunctions("align-all-functions", |
| 83 | cl::desc("Force the alignment of all functions."), |
| 84 | cl::init(0), cl::Hidden); |
Chad Rosier | 8b2e79c | 2015-12-29 18:18:07 +0000 | [diff] [blame] | 85 | |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 86 | static const char *getPropertyName(MachineFunctionProperties::Property Prop) { |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 87 | using P = MachineFunctionProperties::Property; |
| 88 | |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 89 | switch(Prop) { |
Quentin Colombet | 6dff9d7 | 2016-08-26 23:49:01 +0000 | [diff] [blame] | 90 | case P::FailedISel: return "FailedISel"; |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 91 | case P::IsSSA: return "IsSSA"; |
| 92 | case P::Legalized: return "Legalized"; |
Matthias Braun | db9ce2f | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 93 | case P::NoPHIs: return "NoPHIs"; |
Matthias Braun | 690a3cb | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 94 | case P::NoVRegs: return "NoVRegs"; |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 95 | case P::RegBankSelected: return "RegBankSelected"; |
| 96 | case P::Selected: return "Selected"; |
| 97 | case P::TracksLiveness: return "TracksLiveness"; |
Derek Schuff | d239bc5 | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 98 | } |
Matthias Braun | 8b3f754 | 2016-08-19 23:03:28 +0000 | [diff] [blame] | 99 | llvm_unreachable("Invalid machine function property"); |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Aditya Nandakumar | 3b41774 | 2018-09-20 23:01:56 +0000 | [diff] [blame] | 102 | // Pin the vtable to this file. |
| 103 | void MachineFunction::Delegate::anchor() {} |
| 104 | |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 105 | void MachineFunctionProperties::print(raw_ostream &OS) const { |
| 106 | const char *Separator = ""; |
| 107 | for (BitVector::size_type I = 0; I < Properties.size(); ++I) { |
| 108 | if (!Properties[I]) |
| 109 | continue; |
| 110 | OS << Separator << getPropertyName(static_cast<Property>(I)); |
| 111 | Separator = ", "; |
| 112 | } |
Derek Schuff | d239bc5 | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | b84822f | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 116 | // MachineFunction implementation |
Chris Lattner | b84822f | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 117 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9d5d759 | 2005-01-29 18:41:25 +0000 | [diff] [blame] | 118 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 119 | // Out-of-line virtual method. |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 120 | MachineFunctionInfo::~MachineFunctionInfo() = default; |
Chris Lattner | a70e2e3 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 121 | |
Duncan P. N. Exon Smith | 9c80b35 | 2016-08-30 18:40:47 +0000 | [diff] [blame] | 122 | void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 123 | MBB->getParent()->DeleteMachineBasicBlock(MBB); |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 125 | |
Charles Davis | c15a907 | 2016-04-09 23:34:42 +0000 | [diff] [blame] | 126 | static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 127 | const Function &F) { |
| 128 | if (F.hasFnAttribute(Attribute::StackAlignment)) |
| 129 | return F.getFnStackAlignment(); |
Charles Davis | c15a907 | 2016-04-09 23:34:42 +0000 | [diff] [blame] | 130 | return STI->getFrameLowering()->getStackAlignment(); |
| 131 | } |
| 132 | |
Matthias Braun | dad0df6 | 2018-11-05 23:49:14 +0000 | [diff] [blame] | 133 | MachineFunction::MachineFunction(const Function &F, |
| 134 | const LLVMTargetMachine &Target, |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 135 | const TargetSubtargetInfo &STI, |
Rafael Espindola | d1494d5 | 2014-10-14 18:53:16 +0000 | [diff] [blame] | 136 | unsigned FunctionNum, MachineModuleInfo &mmi) |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 137 | : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { |
Quentin Colombet | ac0c6d3 | 2016-08-26 22:32:53 +0000 | [diff] [blame] | 138 | FunctionNumber = FunctionNum; |
| 139 | init(); |
| 140 | } |
| 141 | |
Aditya Nandakumar | 66f2370 | 2019-01-16 00:40:37 +0000 | [diff] [blame] | 142 | void MachineFunction::handleInsertion(MachineInstr &MI) { |
Aditya Nandakumar | 3b41774 | 2018-09-20 23:01:56 +0000 | [diff] [blame] | 143 | if (TheDelegate) |
| 144 | TheDelegate->MF_HandleInsertion(MI); |
| 145 | } |
| 146 | |
Aditya Nandakumar | 66f2370 | 2019-01-16 00:40:37 +0000 | [diff] [blame] | 147 | void MachineFunction::handleRemoval(MachineInstr &MI) { |
Aditya Nandakumar | 3b41774 | 2018-09-20 23:01:56 +0000 | [diff] [blame] | 148 | if (TheDelegate) |
| 149 | TheDelegate->MF_HandleRemoval(MI); |
| 150 | } |
| 151 | |
Quentin Colombet | ac0c6d3 | 2016-08-26 22:32:53 +0000 | [diff] [blame] | 152 | void MachineFunction::init() { |
Derek Schuff | 40e099b | 2016-04-11 23:32:13 +0000 | [diff] [blame] | 153 | // Assume the function starts in SSA form with correct liveness. |
Derek Schuff | cfaa4e0 | 2016-04-04 18:03:29 +0000 | [diff] [blame] | 154 | Properties.set(MachineFunctionProperties::Property::IsSSA); |
Derek Schuff | 40e099b | 2016-04-11 23:32:13 +0000 | [diff] [blame] | 155 | Properties.set(MachineFunctionProperties::Property::TracksLiveness); |
Eric Christopher | c720405 | 2014-10-08 07:51:41 +0000 | [diff] [blame] | 156 | if (STI->getRegisterInfo()) |
Eric Christopher | 99cd10f | 2014-08-12 08:00:56 +0000 | [diff] [blame] | 157 | RegInfo = new (Allocator) MachineRegisterInfo(this); |
Matthijs Kooijman | e2b997b | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 158 | else |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 159 | RegInfo = nullptr; |
Bill Wendling | d10fa8b | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 160 | |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 161 | MFInfo = nullptr; |
Reid Kleckner | 67e7463 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 162 | // We can realign the stack if the target supports it and the user hasn't |
| 163 | // explicitly asked us not to. |
| 164 | bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 165 | !F.hasFnAttribute("no-realign-stack"); |
Reid Kleckner | 67e7463 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 166 | FrameInfo = new (Allocator) MachineFrameInfo( |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 167 | getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, |
Reid Kleckner | 67e7463 | 2016-04-11 17:54:03 +0000 | [diff] [blame] | 168 | /*ForceRealign=*/CanRealignSP && |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 169 | F.hasFnAttribute(Attribute::StackAlignment)); |
Bill Wendling | d10fa8b | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 170 | |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 171 | if (F.hasFnAttribute(Attribute::StackAlignment)) |
| 172 | FrameInfo->ensureMaxAlignment(F.getFnStackAlignment()); |
Bill Wendling | d10fa8b | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 173 | |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 174 | ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); |
Eric Christopher | c720405 | 2014-10-08 07:51:41 +0000 | [diff] [blame] | 175 | Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); |
Bill Wendling | d10fa8b | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 176 | |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 177 | // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. |
Sanjay Patel | f360983 | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 178 | // FIXME: Use Function::optForSize(). |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 179 | if (!F.hasFnAttribute(Attribute::OptimizeForSize)) |
Eric Christopher | c720405 | 2014-10-08 07:51:41 +0000 | [diff] [blame] | 180 | Alignment = std::max(Alignment, |
| 181 | STI->getTargetLowering()->getPrefFunctionAlignment()); |
Bill Wendling | d10fa8b | 2013-06-17 20:41:25 +0000 | [diff] [blame] | 182 | |
Chad Rosier | 8b2e79c | 2015-12-29 18:18:07 +0000 | [diff] [blame] | 183 | if (AlignAllFunctions) |
| 184 | Alignment = AlignAllFunctions; |
| 185 | |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 186 | JumpTableInfo = nullptr; |
Tobias Grosser | 378e97e | 2015-08-17 10:58:03 +0000 | [diff] [blame] | 187 | |
Reid Kleckner | 595419d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 188 | if (isFuncletEHPersonality(classifyEHPersonality( |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 189 | F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { |
Reid Kleckner | 595419d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 190 | WinEHInfo = new (Allocator) WinEHFuncInfo(); |
| 191 | } |
| 192 | |
Heejin Ahn | c430c83 | 2018-06-19 00:26:39 +0000 | [diff] [blame] | 193 | if (isScopedEHPersonality(classifyEHPersonality( |
| 194 | F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { |
| 195 | WasmEHInfo = new (Allocator) WasmEHFuncInfo(); |
| 196 | } |
| 197 | |
Quentin Colombet | ac0c6d3 | 2016-08-26 22:32:53 +0000 | [diff] [blame] | 198 | assert(Target.isCompatibleDataLayout(getDataLayout()) && |
Tobias Grosser | 378e97e | 2015-08-17 10:58:03 +0000 | [diff] [blame] | 199 | "Can't create a MachineFunction using a Module with a " |
| 200 | "Target-incompatible DataLayout attached\n"); |
| 201 | |
Jan Sjodin | 028255f | 2017-09-14 20:53:51 +0000 | [diff] [blame] | 202 | PSVManager = |
| 203 | llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget(). |
| 204 | getInstrInfo())); |
Chris Lattner | 831fdcf | 2002-12-25 05:03:22 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 207 | MachineFunction::~MachineFunction() { |
Quentin Colombet | ac0c6d3 | 2016-08-26 22:32:53 +0000 | [diff] [blame] | 208 | clear(); |
| 209 | } |
| 210 | |
| 211 | void MachineFunction::clear() { |
| 212 | Properties.reset(); |
Jakob Stoklund Olesen | 84be3d5 | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 213 | // Don't call destructors on MachineInstr and MachineOperand. All of their |
| 214 | // memory comes from the BumpPtrAllocator which is about to be purged. |
| 215 | // |
| 216 | // Do call MachineBasicBlock destructors, it contains std::vectors. |
| 217 | for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) |
| 218 | I->Insts.clearAndLeakNodesUnsafely(); |
Roman Tereshin | be8f7c9 | 2018-04-30 18:58:57 +0000 | [diff] [blame] | 219 | MBBNumbering.clear(); |
Jakob Stoklund Olesen | 84be3d5 | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 220 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 221 | InstructionRecycler.clear(Allocator); |
Jakob Stoklund Olesen | f1d015f | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 222 | OperandRecycler.clear(Allocator); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 223 | BasicBlockRecycler.clear(Allocator); |
Reid Kleckner | c86178e | 2017-09-05 20:14:58 +0000 | [diff] [blame] | 224 | CodeViewAnnotations.clear(); |
Tim Northover | dd48fdc | 2017-03-09 21:12:06 +0000 | [diff] [blame] | 225 | VariableDbgInfos.clear(); |
Bill Wendling | dd37b36 | 2009-06-25 00:32:48 +0000 | [diff] [blame] | 226 | if (RegInfo) { |
| 227 | RegInfo->~MachineRegisterInfo(); |
| 228 | Allocator.Deallocate(RegInfo); |
| 229 | } |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 230 | if (MFInfo) { |
Bill Wendling | dd37b36 | 2009-06-25 00:32:48 +0000 | [diff] [blame] | 231 | MFInfo->~MachineFunctionInfo(); |
| 232 | Allocator.Deallocate(MFInfo); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 233 | } |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 234 | |
| 235 | FrameInfo->~MachineFrameInfo(); |
| 236 | Allocator.Deallocate(FrameInfo); |
| 237 | |
| 238 | ConstantPool->~MachineConstantPool(); |
| 239 | Allocator.Deallocate(ConstantPool); |
| 240 | |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 241 | if (JumpTableInfo) { |
| 242 | JumpTableInfo->~MachineJumpTableInfo(); |
| 243 | Allocator.Deallocate(JumpTableInfo); |
| 244 | } |
Reid Kleckner | 595419d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 245 | |
| 246 | if (WinEHInfo) { |
| 247 | WinEHInfo->~WinEHFuncInfo(); |
| 248 | Allocator.Deallocate(WinEHInfo); |
| 249 | } |
Heejin Ahn | 5529711 | 2018-09-28 20:54:04 +0000 | [diff] [blame] | 250 | |
| 251 | if (WasmEHInfo) { |
| 252 | WasmEHInfo->~WasmEHFuncInfo(); |
| 253 | Allocator.Deallocate(WasmEHInfo); |
| 254 | } |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 257 | const DataLayout &MachineFunction::getDataLayout() const { |
Matthias Braun | dfcb4f5 | 2017-12-15 22:22:46 +0000 | [diff] [blame] | 258 | return F.getParent()->getDataLayout(); |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 261 | /// Get the JumpTableInfo for this function. |
| 262 | /// If it does not already exist, allocate one. |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 263 | MachineJumpTableInfo *MachineFunction:: |
| 264 | getOrCreateJumpTableInfo(unsigned EntryKind) { |
| 265 | if (JumpTableInfo) return JumpTableInfo; |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 266 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 267 | JumpTableInfo = new (Allocator) |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 268 | MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); |
| 269 | return JumpTableInfo; |
| 270 | } |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 271 | |
Reid Kleckner | bc1fd91 | 2014-04-10 22:58:43 +0000 | [diff] [blame] | 272 | /// Should we be emitting segmented stack stuff for the function |
Quentin Colombet | 871b3df | 2016-01-19 22:31:12 +0000 | [diff] [blame] | 273 | bool MachineFunction::shouldSplitStack() const { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 274 | return getFunction().hasFnAttribute("split-stack"); |
Reid Kleckner | bc1fd91 | 2014-04-10 22:58:43 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 277 | /// This discards all of the MachineBasicBlock numbers and recomputes them. |
| 278 | /// This guarantees that the MBB numbers are sequential, dense, and match the |
| 279 | /// ordering of the blocks within the function. If a specific MachineBasicBlock |
| 280 | /// is specified, only that block and those after it are renumbered. |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 281 | void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { |
| 282 | if (empty()) { MBBNumbering.clear(); return; } |
| 283 | MachineFunction::iterator MBBI, E = end(); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 284 | if (MBB == nullptr) |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 285 | MBBI = begin(); |
| 286 | else |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 287 | MBBI = MBB->getIterator(); |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 288 | |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 289 | // Figure out the block number this should have. |
| 290 | unsigned BlockNo = 0; |
Chris Lattner | f28bbda | 2006-10-03 20:19:23 +0000 | [diff] [blame] | 291 | if (MBBI != begin()) |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 292 | BlockNo = std::prev(MBBI)->getNumber() + 1; |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 293 | |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 294 | for (; MBBI != E; ++MBBI, ++BlockNo) { |
| 295 | if (MBBI->getNumber() != (int)BlockNo) { |
| 296 | // Remove use of the old number. |
| 297 | if (MBBI->getNumber() != -1) { |
| 298 | assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && |
| 299 | "MBB number mismatch!"); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 300 | MBBNumbering[MBBI->getNumber()] = nullptr; |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 301 | } |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 302 | |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 303 | // If BlockNo is already taken, set that block's number to -1. |
| 304 | if (MBBNumbering[BlockNo]) |
| 305 | MBBNumbering[BlockNo]->setNumber(-1); |
| 306 | |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 307 | MBBNumbering[BlockNo] = &*MBBI; |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 308 | MBBI->setNumber(BlockNo); |
| 309 | } |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 310 | } |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 311 | |
| 312 | // Okay, all the blocks are renumbered. If we have compactified the block |
| 313 | // numbering, shrink MBBNumbering now. |
| 314 | assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); |
| 315 | MBBNumbering.resize(BlockNo); |
| 316 | } |
| 317 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 318 | /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. |
Benjamin Kramer | af18e01 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 319 | MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, |
| 320 | const DebugLoc &DL, |
| 321 | bool NoImp) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 322 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
Jakob Stoklund Olesen | 9500e5d | 2012-12-20 22:53:58 +0000 | [diff] [blame] | 323 | MachineInstr(*this, MCID, DL, NoImp); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 325 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 326 | /// Create a new MachineInstr which is a copy of the 'Orig' instruction, |
| 327 | /// identical in all ways except the instruction has no parent, prev, or next. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 328 | MachineInstr * |
| 329 | MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { |
| 330 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
| 331 | MachineInstr(*this, *Orig); |
| 332 | } |
| 333 | |
Matthias Braun | 18f2445 | 2017-08-22 23:56:30 +0000 | [diff] [blame] | 334 | MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, |
| 335 | MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { |
| 336 | MachineInstr *FirstClone = nullptr; |
| 337 | MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 338 | while (true) { |
Matthias Braun | 18f2445 | 2017-08-22 23:56:30 +0000 | [diff] [blame] | 339 | MachineInstr *Cloned = CloneMachineInstr(&*I); |
| 340 | MBB.insert(InsertBefore, Cloned); |
| 341 | if (FirstClone == nullptr) { |
| 342 | FirstClone = Cloned; |
| 343 | } else { |
| 344 | Cloned->bundleWithPred(); |
| 345 | } |
| 346 | |
| 347 | if (!I->isBundledWithSucc()) |
| 348 | break; |
| 349 | ++I; |
| 350 | } |
| 351 | return *FirstClone; |
| 352 | } |
| 353 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 354 | /// Delete the given MachineInstr. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 355 | /// |
Jakob Stoklund Olesen | 84be3d5 | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 356 | /// This function also serves as the MachineInstr destructor - the real |
| 357 | /// ~MachineInstr() destructor must be empty. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 358 | void |
| 359 | MachineFunction::DeleteMachineInstr(MachineInstr *MI) { |
Jakob Stoklund Olesen | f1d015f | 2013-01-05 05:00:09 +0000 | [diff] [blame] | 360 | // Strip it for parts. The operand array and the MI object itself are |
| 361 | // independently recyclable. |
| 362 | if (MI->Operands) |
| 363 | deallocateOperandArray(MI->CapOperands, MI->Operands); |
Jakob Stoklund Olesen | 84be3d5 | 2013-01-05 05:05:51 +0000 | [diff] [blame] | 364 | // Don't call ~MachineInstr() which must be trivial anyway because |
| 365 | // ~MachineFunction drops whole lists of MachineInstrs wihout calling their |
| 366 | // destructors. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 367 | InstructionRecycler.Deallocate(Allocator, MI); |
| 368 | } |
| 369 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 370 | /// Allocate a new MachineBasicBlock. Use this instead of |
| 371 | /// `new MachineBasicBlock'. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 372 | MachineBasicBlock * |
| 373 | MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { |
| 374 | return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) |
| 375 | MachineBasicBlock(*this, bb); |
| 376 | } |
| 377 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 378 | /// Delete the given MachineBasicBlock. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 379 | void |
| 380 | MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { |
| 381 | assert(MBB->getParent() == this && "MBB parent mismatch!"); |
| 382 | MBB->~MachineBasicBlock(); |
| 383 | BasicBlockRecycler.Deallocate(Allocator, MBB); |
| 384 | } |
| 385 | |
Justin Lebar | 14fc45e | 2016-07-15 18:26:59 +0000 | [diff] [blame] | 386 | MachineMemOperand *MachineFunction::getMachineMemOperand( |
| 387 | MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 388 | unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 389 | SyncScope::ID SSID, AtomicOrdering Ordering, |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 390 | AtomicOrdering FailureOrdering) { |
Justin Lebar | f66cee6 | 2016-07-14 17:07:44 +0000 | [diff] [blame] | 391 | return new (Allocator) |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 392 | MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 393 | SSID, Ordering, FailureOrdering); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | MachineMemOperand * |
| 397 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
| 398 | int64_t Offset, uint64_t Size) { |
Nick Lewycky | d63390c | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 399 | if (MMO->getValue()) |
| 400 | return new (Allocator) |
| 401 | MachineMemOperand(MachinePointerInfo(MMO->getValue(), |
| 402 | MMO->getOffset()+Offset), |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 403 | MMO->getFlags(), Size, MMO->getBaseAlignment(), |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 404 | AAMDNodes(), nullptr, MMO->getSyncScopeID(), |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 405 | MMO->getOrdering(), MMO->getFailureOrdering()); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 406 | return new (Allocator) |
Nick Lewycky | d63390c | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 407 | MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(), |
Chris Lattner | da39c39 | 2010-09-21 04:32:08 +0000 | [diff] [blame] | 408 | MMO->getOffset()+Offset), |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 409 | MMO->getFlags(), Size, MMO->getBaseAlignment(), |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 410 | AAMDNodes(), nullptr, MMO->getSyncScopeID(), |
Konstantin Zhuravlyov | a7e2c08 | 2016-10-15 22:01:18 +0000 | [diff] [blame] | 411 | MMO->getOrdering(), MMO->getFailureOrdering()); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 414 | MachineMemOperand * |
| 415 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
| 416 | const AAMDNodes &AAInfo) { |
| 417 | MachinePointerInfo MPI = MMO->getValue() ? |
| 418 | MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : |
| 419 | MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); |
| 420 | |
| 421 | return new (Allocator) |
| 422 | MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(), |
| 423 | MMO->getBaseAlignment(), AAInfo, |
| 424 | MMO->getRanges(), MMO->getSyncScopeID(), |
| 425 | MMO->getOrdering(), MMO->getFailureOrdering()); |
| 426 | } |
| 427 | |
Chandler Carruth | 2a752bf | 2018-08-16 21:30:05 +0000 | [diff] [blame] | 428 | MachineInstr::ExtraInfo * |
| 429 | MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs, |
| 430 | MCSymbol *PreInstrSymbol, |
| 431 | MCSymbol *PostInstrSymbol) { |
| 432 | return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, |
| 433 | PostInstrSymbol); |
Dan Gohman | 91e69c3 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Alex Lorenz | 65671bf | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 436 | const char *MachineFunction::createExternalSymbolName(StringRef Name) { |
| 437 | char *Dest = Allocator.Allocate<char>(Name.size() + 1); |
Fangrui Song | 53a6224 | 2018-11-17 01:44:25 +0000 | [diff] [blame] | 438 | llvm::copy(Name, Dest); |
Alex Lorenz | 65671bf | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 439 | Dest[Name.size()] = 0; |
| 440 | return Dest; |
| 441 | } |
| 442 | |
Matthias Braun | ee571f2 | 2018-07-26 00:27:47 +0000 | [diff] [blame] | 443 | uint32_t *MachineFunction::allocateRegMask() { |
| 444 | unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); |
| 445 | unsigned Size = MachineOperand::getRegMaskSize(NumRegs); |
| 446 | uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); |
| 447 | memset(Mask, 0, Size * sizeof(Mask[0])); |
| 448 | return Mask; |
| 449 | } |
| 450 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 451 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 452 | LLVM_DUMP_METHOD void MachineFunction::dump() const { |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 453 | print(dbgs()); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 454 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 455 | #endif |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 456 | |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 457 | StringRef MachineFunction::getName() const { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 458 | return getFunction().getName(); |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Matthias Braun | d170aea | 2016-05-05 18:14:43 +0000 | [diff] [blame] | 461 | void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 462 | OS << "# Machine code for function " << getName() << ": "; |
Derek Schuff | d239bc5 | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 463 | getProperties().print(OS); |
Matthias Braun | 2b72738 | 2016-09-29 01:47:42 +0000 | [diff] [blame] | 464 | OS << '\n'; |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 465 | |
| 466 | // Print Frame Information |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 467 | FrameInfo->print(*this, OS); |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 468 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 469 | // Print JumpTable Information |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 470 | if (JumpTableInfo) |
| 471 | JumpTableInfo->print(OS); |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 472 | |
| 473 | // Print Constant Pool |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 474 | ConstantPool->print(OS); |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 475 | |
Eric Christopher | 6035518 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 476 | const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 477 | |
Matthijs Kooijman | e2b997b | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 478 | if (RegInfo && !RegInfo->livein_empty()) { |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 479 | OS << "Function Live Ins: "; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 480 | for (MachineRegisterInfo::livein_iterator |
| 481 | I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 482 | OS << printReg(I->first, TRI); |
Chris Lattner | 4e92027 | 2006-05-16 05:55:30 +0000 | [diff] [blame] | 483 | if (I->second) |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 484 | OS << " in " << printReg(I->second, TRI); |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 485 | if (std::next(I) != E) |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 486 | OS << ", "; |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 487 | } |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 488 | OS << '\n'; |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 489 | } |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 490 | |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 491 | ModuleSlotTracker MST(getFunction().getParent()); |
| 492 | MST.incorporateFunction(getFunction()); |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 493 | for (const auto &BB : *this) { |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 494 | OS << '\n'; |
Francis Visoiu Mistrih | 454e6dc | 2018-02-26 15:23:42 +0000 | [diff] [blame] | 495 | // If we print the whole function, print it at its most verbose level. |
| 496 | BB.print(OS, MST, Indexes, /*IsStandalone=*/true); |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 497 | } |
Brian Gaeke | 47b7164 | 2004-03-29 21:58:31 +0000 | [diff] [blame] | 498 | |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 499 | OS << "\n# End machine code for function " << getName() << ".\n\n"; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 502 | namespace llvm { |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 503 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 504 | template<> |
| 505 | struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 506 | DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 507 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 508 | static std::string getGraphName(const MachineFunction *F) { |
Yaron Keren | 2a9bbcb | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 509 | return ("CFG for '" + F->getName() + "' function").str(); |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 512 | std::string getNodeLabel(const MachineBasicBlock *Node, |
| 513 | const MachineFunction *Graph) { |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 514 | std::string OutStr; |
| 515 | { |
| 516 | raw_string_ostream OSS(OutStr); |
Jakob Stoklund Olesen | e5f4e9f | 2010-10-30 01:26:19 +0000 | [diff] [blame] | 517 | |
| 518 | if (isSimple()) { |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 519 | OSS << printMBBReference(*Node); |
Jakob Stoklund Olesen | e5f4e9f | 2010-10-30 01:26:19 +0000 | [diff] [blame] | 520 | if (const BasicBlock *BB = Node->getBasicBlock()) |
| 521 | OSS << ": " << BB->getName(); |
| 522 | } else |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 523 | Node->print(OSS); |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 524 | } |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 525 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 526 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 527 | |
| 528 | // Process string output to make it nicer... |
| 529 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 530 | if (OutStr[i] == '\n') { // Left justify |
| 531 | OutStr[i] = '\\'; |
| 532 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 533 | } |
| 534 | return OutStr; |
| 535 | } |
| 536 | }; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 537 | |
| 538 | } // end namespace llvm |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 539 | |
| 540 | void MachineFunction::viewCFG() const |
| 541 | { |
Jim Laskey | 851a22d | 2005-10-12 12:09:05 +0000 | [diff] [blame] | 542 | #ifndef NDEBUG |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 543 | ViewGraph(this, "mf" + getName()); |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 544 | #else |
Dan Gohman | 643fffe | 2010-07-07 17:28:45 +0000 | [diff] [blame] | 545 | errs() << "MachineFunction::viewCFG is only available in debug builds on " |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 546 | << "systems with Graphviz or gv!\n"; |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 547 | #endif // NDEBUG |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void MachineFunction::viewCFGOnly() const |
| 551 | { |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 552 | #ifndef NDEBUG |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 553 | ViewGraph(this, "mf" + getName(), true); |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 554 | #else |
Dan Gohman | 643fffe | 2010-07-07 17:28:45 +0000 | [diff] [blame] | 555 | errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 556 | << "systems with Graphviz or gv!\n"; |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 557 | #endif // NDEBUG |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 560 | /// Add the specified physical register as a live-in value and |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 561 | /// create a corresponding virtual register for it. |
| 562 | unsigned MachineFunction::addLiveIn(unsigned PReg, |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 563 | const TargetRegisterClass *RC) { |
Evan Cheng | 3946043 | 2010-05-24 21:33:37 +0000 | [diff] [blame] | 564 | MachineRegisterInfo &MRI = getRegInfo(); |
| 565 | unsigned VReg = MRI.getLiveInVirtReg(PReg); |
| 566 | if (VReg) { |
Quentin Colombet | b0f8afd | 2013-12-12 00:15:47 +0000 | [diff] [blame] | 567 | const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); |
| 568 | (void)VRegRC; |
| 569 | // A physical register can be added several times. |
| 570 | // Between two calls, the register class of the related virtual register |
| 571 | // may have been constrained to match some operation constraints. |
| 572 | // In that case, check that the current register class includes the |
| 573 | // physical register and is a sub class of the specified RC. |
| 574 | assert((VRegRC == RC || (VRegRC->contains(PReg) && |
| 575 | RC->hasSubClassEq(VRegRC))) && |
| 576 | "Register class mismatch!"); |
Evan Cheng | 3946043 | 2010-05-24 21:33:37 +0000 | [diff] [blame] | 577 | return VReg; |
| 578 | } |
| 579 | VReg = MRI.createVirtualRegister(RC); |
| 580 | MRI.addLiveIn(PReg, VReg); |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 581 | return VReg; |
| 582 | } |
| 583 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 584 | /// Return the MCSymbol for the specified non-empty jump table. |
Bill Wendling | 07d3177 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 585 | /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a |
| 586 | /// normal 'L' label is returned. |
NAKAMURA Takumi | 2fc4d99 | 2014-06-25 12:40:56 +0000 | [diff] [blame] | 587 | MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, |
Bill Wendling | 07d3177 | 2010-06-29 22:34:52 +0000 | [diff] [blame] | 588 | bool isLinkerPrivate) const { |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 589 | const DataLayout &DL = getDataLayout(); |
Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 590 | assert(JumpTableInfo && "No jump tables"); |
Chandler Carruth | de4c080 | 2010-01-27 10:27:10 +0000 | [diff] [blame] | 591 | assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 592 | |
Mehdi Amini | 0efcb40 | 2016-10-01 05:57:55 +0000 | [diff] [blame] | 593 | StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() |
| 594 | : DL.getPrivateGlobalPrefix(); |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 595 | SmallString<60> Name; |
| 596 | raw_svector_ostream(Name) |
| 597 | << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; |
Jim Grosbach | 19696da | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 598 | return Ctx.getOrCreateSymbol(Name); |
Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 601 | /// Return a function-local symbol to represent the PIC base. |
Chris Lattner | 142b531 | 2010-11-14 22:48:15 +0000 | [diff] [blame] | 602 | MCSymbol *MachineFunction::getPICBaseSymbol() const { |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 603 | const DataLayout &DL = getDataLayout(); |
| 604 | return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + |
| 605 | Twine(getFunctionNumber()) + "$pb"); |
Chris Lattner | 142b531 | 2010-11-14 22:48:15 +0000 | [diff] [blame] | 606 | } |
Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 607 | |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 608 | /// \name Exception Handling |
| 609 | /// \{ |
| 610 | |
| 611 | LandingPadInfo & |
| 612 | MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { |
| 613 | unsigned N = LandingPads.size(); |
| 614 | for (unsigned i = 0; i < N; ++i) { |
| 615 | LandingPadInfo &LP = LandingPads[i]; |
| 616 | if (LP.LandingPadBlock == LandingPad) |
| 617 | return LP; |
| 618 | } |
| 619 | |
| 620 | LandingPads.push_back(LandingPadInfo(LandingPad)); |
| 621 | return LandingPads[N]; |
| 622 | } |
| 623 | |
| 624 | void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, |
| 625 | MCSymbol *BeginLabel, MCSymbol *EndLabel) { |
| 626 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 627 | LP.BeginLabels.push_back(BeginLabel); |
| 628 | LP.EndLabels.push_back(EndLabel); |
| 629 | } |
| 630 | |
| 631 | MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { |
| 632 | MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); |
| 633 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 634 | LP.LandingPadLabel = LandingPadLabel; |
Heejin Ahn | f384f74 | 2018-09-25 19:56:44 +0000 | [diff] [blame] | 635 | |
| 636 | const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); |
| 637 | if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { |
| 638 | if (const auto *PF = |
| 639 | dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) |
| 640 | getMMI().addPersonality(PF); |
| 641 | |
| 642 | if (LPI->isCleanup()) |
| 643 | addCleanup(LandingPad); |
| 644 | |
| 645 | // FIXME: New EH - Add the clauses in reverse order. This isn't 100% |
Heejin Ahn | cde69b2 | 2018-09-29 09:22:25 +0000 | [diff] [blame] | 646 | // correct, but we need to do it this way because of how the DWARF EH |
| 647 | // emitter processes the clauses. |
Heejin Ahn | f384f74 | 2018-09-25 19:56:44 +0000 | [diff] [blame] | 648 | for (unsigned I = LPI->getNumClauses(); I != 0; --I) { |
| 649 | Value *Val = LPI->getClause(I - 1); |
| 650 | if (LPI->isCatch(I - 1)) { |
| 651 | addCatchTypeInfo(LandingPad, |
| 652 | dyn_cast<GlobalValue>(Val->stripPointerCasts())); |
| 653 | } else { |
| 654 | // Add filters in a list. |
| 655 | auto *CVal = cast<Constant>(Val); |
| 656 | SmallVector<const GlobalValue *, 4> FilterList; |
| 657 | for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end(); |
| 658 | II != IE; ++II) |
| 659 | FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); |
| 660 | |
| 661 | addFilterTypeInfo(LandingPad, FilterList); |
| 662 | } |
| 663 | } |
| 664 | |
Heejin Ahn | 397841e | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 665 | } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { |
| 666 | for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { |
| 667 | Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); |
| 668 | addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); |
| 669 | } |
Heejin Ahn | f384f74 | 2018-09-25 19:56:44 +0000 | [diff] [blame] | 670 | |
| 671 | } else { |
| 672 | assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); |
| 673 | } |
| 674 | |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 675 | return LandingPadLabel; |
| 676 | } |
| 677 | |
| 678 | void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, |
| 679 | ArrayRef<const GlobalValue *> TyInfo) { |
| 680 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 681 | for (unsigned N = TyInfo.size(); N; --N) |
| 682 | LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); |
| 683 | } |
| 684 | |
| 685 | void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, |
| 686 | ArrayRef<const GlobalValue *> TyInfo) { |
| 687 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 688 | std::vector<unsigned> IdsInFilter(TyInfo.size()); |
| 689 | for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) |
| 690 | IdsInFilter[I] = getTypeIDFor(TyInfo[I]); |
| 691 | LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); |
| 692 | } |
| 693 | |
Heejin Ahn | 397841e | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 694 | void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, |
| 695 | bool TidyIfNoBeginLabels) { |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 696 | for (unsigned i = 0; i != LandingPads.size(); ) { |
| 697 | LandingPadInfo &LandingPad = LandingPads[i]; |
| 698 | if (LandingPad.LandingPadLabel && |
| 699 | !LandingPad.LandingPadLabel->isDefined() && |
| 700 | (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) |
| 701 | LandingPad.LandingPadLabel = nullptr; |
| 702 | |
| 703 | // Special case: we *should* emit LPs with null LP MBB. This indicates |
| 704 | // "nounwind" case. |
| 705 | if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { |
| 706 | LandingPads.erase(LandingPads.begin() + i); |
| 707 | continue; |
| 708 | } |
| 709 | |
Heejin Ahn | 397841e | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 710 | if (TidyIfNoBeginLabels) { |
| 711 | for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { |
| 712 | MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; |
| 713 | MCSymbol *EndLabel = LandingPad.EndLabels[j]; |
| 714 | if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && |
| 715 | (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) |
| 716 | continue; |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 717 | |
Heejin Ahn | 397841e | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 718 | LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); |
| 719 | LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); |
| 720 | --j; |
| 721 | --e; |
| 722 | } |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 723 | |
Heejin Ahn | 397841e | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 724 | // Remove landing pads with no try-ranges. |
| 725 | if (LandingPads[i].BeginLabels.empty()) { |
| 726 | LandingPads.erase(LandingPads.begin() + i); |
| 727 | continue; |
| 728 | } |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // If there is no landing pad, ensure that the list of typeids is empty. |
| 732 | // If the only typeid is a cleanup, this is the same as having no typeids. |
| 733 | if (!LandingPad.LandingPadBlock || |
| 734 | (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) |
| 735 | LandingPad.TypeIds.clear(); |
| 736 | ++i; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { |
| 741 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 742 | LP.TypeIds.push_back(0); |
| 743 | } |
| 744 | |
| 745 | void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, |
| 746 | const Function *Filter, |
| 747 | const BlockAddress *RecoverBA) { |
| 748 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 749 | SEHHandler Handler; |
| 750 | Handler.FilterOrFinally = Filter; |
| 751 | Handler.RecoverBA = RecoverBA; |
| 752 | LP.SEHHandlers.push_back(Handler); |
| 753 | } |
| 754 | |
| 755 | void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, |
| 756 | const Function *Cleanup) { |
| 757 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 758 | SEHHandler Handler; |
| 759 | Handler.FilterOrFinally = Cleanup; |
| 760 | Handler.RecoverBA = nullptr; |
| 761 | LP.SEHHandlers.push_back(Handler); |
| 762 | } |
| 763 | |
| 764 | void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, |
| 765 | ArrayRef<unsigned> Sites) { |
| 766 | LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); |
| 767 | } |
| 768 | |
| 769 | unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { |
| 770 | for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) |
| 771 | if (TypeInfos[i] == TI) return i + 1; |
| 772 | |
| 773 | TypeInfos.push_back(TI); |
| 774 | return TypeInfos.size(); |
| 775 | } |
| 776 | |
| 777 | int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { |
| 778 | // If the new filter coincides with the tail of an existing filter, then |
| 779 | // re-use the existing filter. Folding filters more than this requires |
| 780 | // re-ordering filters and/or their elements - probably not worth it. |
| 781 | for (std::vector<unsigned>::iterator I = FilterEnds.begin(), |
| 782 | E = FilterEnds.end(); I != E; ++I) { |
| 783 | unsigned i = *I, j = TyIds.size(); |
| 784 | |
| 785 | while (i && j) |
| 786 | if (FilterIds[--i] != TyIds[--j]) |
| 787 | goto try_next; |
| 788 | |
| 789 | if (!j) |
| 790 | // The new filter coincides with range [i, end) of the existing filter. |
| 791 | return -(1 + i); |
| 792 | |
| 793 | try_next:; |
| 794 | } |
| 795 | |
| 796 | // Add the new filter. |
| 797 | int FilterID = -(1 + FilterIds.size()); |
| 798 | FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); |
| 799 | FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end()); |
| 800 | FilterEnds.push_back(FilterIds.size()); |
| 801 | FilterIds.push_back(0); // terminator |
| 802 | return FilterID; |
| 803 | } |
| 804 | |
Matthias Braun | 347847b | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 805 | /// \} |
| 806 | |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 807 | //===----------------------------------------------------------------------===// |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 808 | // MachineJumpTableInfo implementation |
| 809 | //===----------------------------------------------------------------------===// |
| 810 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 811 | /// Return the size of each entry in the jump table. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 812 | unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 813 | // The size of a jump table entry is 4 bytes unless the entry is just the |
| 814 | // address of a block, in which case it is the pointer size. |
| 815 | switch (getEntryKind()) { |
| 816 | case MachineJumpTableInfo::EK_BlockAddress: |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 817 | return TD.getPointerSize(); |
Akira Hatanaka | 6c2cf8b | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 818 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
| 819 | return 8; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 820 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 821 | case MachineJumpTableInfo::EK_LabelDifference32: |
Chris Lattner | 85fe078 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 822 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 823 | return 4; |
Richard Osborne | 95da605 | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 824 | case MachineJumpTableInfo::EK_Inline: |
| 825 | return 0; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 826 | } |
Craig Topper | aae875c | 2012-02-06 08:17:43 +0000 | [diff] [blame] | 827 | llvm_unreachable("Unknown jump table encoding!"); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 830 | /// Return the alignment of each entry in the jump table. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 831 | unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 832 | // The alignment of a jump table entry is the alignment of int32 unless the |
| 833 | // entry is just the address of a block, in which case it is the pointer |
| 834 | // alignment. |
| 835 | switch (getEntryKind()) { |
| 836 | case MachineJumpTableInfo::EK_BlockAddress: |
Fangrui Song | 0b5d88f | 2017-11-15 06:17:32 +0000 | [diff] [blame] | 837 | return TD.getPointerABIAlignment(0); |
Akira Hatanaka | 6c2cf8b | 2012-02-03 04:33:00 +0000 | [diff] [blame] | 838 | case MachineJumpTableInfo::EK_GPRel64BlockAddress: |
| 839 | return TD.getABIIntegerTypeAlignment(64); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 840 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 841 | case MachineJumpTableInfo::EK_LabelDifference32: |
Chris Lattner | 85fe078 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 842 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 843 | return TD.getABIIntegerTypeAlignment(32); |
Richard Osborne | 95da605 | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 844 | case MachineJumpTableInfo::EK_Inline: |
| 845 | return 1; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 846 | } |
Craig Topper | aae875c | 2012-02-06 08:17:43 +0000 | [diff] [blame] | 847 | llvm_unreachable("Unknown jump table encoding!"); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 850 | /// Create a new jump table entry in the jump table info. |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 851 | unsigned MachineJumpTableInfo::createJumpTableIndex( |
Chris Lattner | a4eb44a | 2006-10-28 18:17:09 +0000 | [diff] [blame] | 852 | const std::vector<MachineBasicBlock*> &DestBBs) { |
Chris Lattner | e7251a0 | 2006-10-28 18:11:20 +0000 | [diff] [blame] | 853 | assert(!DestBBs.empty() && "Cannot create an empty jump table!"); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 854 | JumpTables.push_back(MachineJumpTableEntry(DestBBs)); |
| 855 | return JumpTables.size()-1; |
| 856 | } |
| 857 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 858 | /// If Old is the target of any jump tables, update the jump tables to branch |
| 859 | /// to New instead. |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 860 | bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, |
| 861 | MachineBasicBlock *New) { |
Dan Gohman | 593ea05 | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 862 | assert(Old != New && "Not making a change?"); |
| 863 | bool MadeChange = false; |
Jim Grosbach | 68bb60f | 2009-11-14 20:09:13 +0000 | [diff] [blame] | 864 | for (size_t i = 0, e = JumpTables.size(); i != e; ++i) |
| 865 | ReplaceMBBInJumpTable(i, Old, New); |
| 866 | return MadeChange; |
| 867 | } |
| 868 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 869 | /// If Old is a target of the jump tables, update the jump table to branch to |
| 870 | /// New instead. |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 871 | bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, |
| 872 | MachineBasicBlock *Old, |
| 873 | MachineBasicBlock *New) { |
Jim Grosbach | 68bb60f | 2009-11-14 20:09:13 +0000 | [diff] [blame] | 874 | assert(Old != New && "Not making a change?"); |
| 875 | bool MadeChange = false; |
| 876 | MachineJumpTableEntry &JTE = JumpTables[Idx]; |
| 877 | for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) |
| 878 | if (JTE.MBBs[j] == Old) { |
| 879 | JTE.MBBs[j] = New; |
| 880 | MadeChange = true; |
| 881 | } |
Dan Gohman | 593ea05 | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 882 | return MadeChange; |
| 883 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 884 | |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 885 | void MachineJumpTableInfo::print(raw_ostream &OS) const { |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 886 | if (JumpTables.empty()) return; |
| 887 | |
| 888 | OS << "Jump Tables:\n"; |
| 889 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 890 | for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { |
Francis Visoiu Mistrih | d347e97 | 2017-12-13 10:30:59 +0000 | [diff] [blame] | 891 | OS << printJumpTableEntryReference(i) << ": "; |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 892 | for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 893 | OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 894 | } |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 895 | |
| 896 | OS << '\n'; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 899 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 900 | LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 901 | #endif |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 902 | |
Francis Visoiu Mistrih | d347e97 | 2017-12-13 10:30:59 +0000 | [diff] [blame] | 903 | Printable llvm::printJumpTableEntryReference(unsigned Idx) { |
| 904 | return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); |
| 905 | } |
| 906 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 907 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 908 | // MachineConstantPool implementation |
| 909 | //===----------------------------------------------------------------------===// |
| 910 | |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 911 | void MachineConstantPoolValue::anchor() {} |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 912 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 913 | Type *MachineConstantPoolEntry::getType() const { |
Evan Cheng | 9abd7c3 | 2006-09-14 05:50:57 +0000 | [diff] [blame] | 914 | if (isMachineConstantPoolEntry()) |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 915 | return Val.MachineCPVal->getType(); |
Evan Cheng | 9abd7c3 | 2006-09-14 05:50:57 +0000 | [diff] [blame] | 916 | return Val.ConstVal->getType(); |
| 917 | } |
| 918 | |
Rafael Espindola | 8dcaa9f | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 919 | bool MachineConstantPoolEntry::needsRelocation() const { |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 920 | if (isMachineConstantPoolEntry()) |
Rafael Espindola | 8dcaa9f | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 921 | return true; |
| 922 | return Val.ConstVal->needsRelocation(); |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 923 | } |
| 924 | |
David Majnemer | a539d7b | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 925 | SectionKind |
| 926 | MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { |
Rafael Espindola | 8dcaa9f | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 927 | if (needsRelocation()) |
| 928 | return SectionKind::getReadOnlyWithRel(); |
| 929 | switch (DL->getTypeAllocSize(getType())) { |
| 930 | case 4: |
| 931 | return SectionKind::getMergeableConst4(); |
| 932 | case 8: |
| 933 | return SectionKind::getMergeableConst8(); |
| 934 | case 16: |
| 935 | return SectionKind::getMergeableConst16(); |
David Majnemer | b23b0ff | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 936 | case 32: |
| 937 | return SectionKind::getMergeableConst32(); |
David Majnemer | a539d7b | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 938 | default: |
Rafael Espindola | 8dcaa9f | 2015-11-17 00:51:23 +0000 | [diff] [blame] | 939 | return SectionKind::getReadOnly(); |
David Majnemer | a539d7b | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 940 | } |
David Majnemer | a539d7b | 2014-07-14 22:06:29 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 943 | MachineConstantPool::~MachineConstantPool() { |
James Molloy | ba54dc2 | 2016-09-26 07:26:24 +0000 | [diff] [blame] | 944 | // A constant may be a member of both Constants and MachineCPVsSharingEntries, |
| 945 | // so keep track of which we've deleted to avoid double deletions. |
| 946 | DenseSet<MachineConstantPoolValue*> Deleted; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 947 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
James Molloy | ba54dc2 | 2016-09-26 07:26:24 +0000 | [diff] [blame] | 948 | if (Constants[i].isMachineConstantPoolEntry()) { |
| 949 | Deleted.insert(Constants[i].Val.MachineCPVal); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 950 | delete Constants[i].Val.MachineCPVal; |
James Molloy | ba54dc2 | 2016-09-26 07:26:24 +0000 | [diff] [blame] | 951 | } |
Cameron Zwarich | 5567869 | 2011-02-22 08:54:30 +0000 | [diff] [blame] | 952 | for (DenseSet<MachineConstantPoolValue*>::iterator I = |
| 953 | MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); |
James Molloy | ba54dc2 | 2016-09-26 07:26:24 +0000 | [diff] [blame] | 954 | I != E; ++I) { |
| 955 | if (Deleted.count(*I) == 0) |
| 956 | delete *I; |
| 957 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 960 | /// Test whether the given two constants can be allocated the same constant pool |
| 961 | /// entry. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 962 | static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 963 | const DataLayout &DL) { |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 964 | // Handle the trivial case quickly. |
| 965 | if (A == B) return true; |
| 966 | |
| 967 | // If they have the same type but weren't the same constant, quickly |
| 968 | // reject them. |
| 969 | if (A->getType() == B->getType()) return false; |
| 970 | |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 971 | // We can't handle structs or arrays. |
| 972 | if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || |
| 973 | isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) |
| 974 | return false; |
NAKAMURA Takumi | 2fc4d99 | 2014-06-25 12:40:56 +0000 | [diff] [blame] | 975 | |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 976 | // For now, only support constants with the same size. |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 977 | uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); |
| 978 | if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 979 | return false; |
| 980 | |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 981 | Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 982 | |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 983 | // Try constant folding a bitcast of both instructions to an integer. If we |
| 984 | // get two identical ConstantInt's, then we are good to share them. We use |
| 985 | // the constant folding APIs to do this so that we get the benefit of |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 986 | // DataLayout. |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 987 | if (isa<PointerType>(A->getType())) |
Manuel Jacob | 9b455a8 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 988 | A = ConstantFoldCastOperand(Instruction::PtrToInt, |
| 989 | const_cast<Constant *>(A), IntTy, DL); |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 990 | else if (A->getType() != IntTy) |
Manuel Jacob | 9b455a8 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 991 | A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), |
| 992 | IntTy, DL); |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 993 | if (isa<PointerType>(B->getType())) |
Manuel Jacob | 9b455a8 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 994 | B = ConstantFoldCastOperand(Instruction::PtrToInt, |
| 995 | const_cast<Constant *>(B), IntTy, DL); |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 996 | else if (B->getType() != IntTy) |
Manuel Jacob | 9b455a8 | 2016-01-21 06:31:08 +0000 | [diff] [blame] | 997 | B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), |
| 998 | IntTy, DL); |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 73858d1 | 2012-01-27 01:46:00 +0000 | [diff] [blame] | 1000 | return A == B; |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Sanjay Patel | ea58c7d | 2015-06-13 15:32:45 +0000 | [diff] [blame] | 1003 | /// Create a new entry in the constant pool or return an existing one. |
| 1004 | /// User must specify the log2 of the minimum required alignment for the object. |
NAKAMURA Takumi | 2fc4d99 | 2014-06-25 12:40:56 +0000 | [diff] [blame] | 1005 | unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1006 | unsigned Alignment) { |
| 1007 | assert(Alignment && "Alignment must be specified!"); |
| 1008 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1010 | // Check to see if we already have this constant. |
| 1011 | // |
| 1012 | // FIXME, this could be made much more efficient for large constant pools. |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1013 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 1014 | if (!Constants[i].isMachineConstantPoolEntry() && |
Mehdi Amini | 33c4d68 | 2015-07-07 18:20:57 +0000 | [diff] [blame] | 1015 | CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 1016 | if ((unsigned)Constants[i].getAlignment() < Alignment) |
| 1017 | Constants[i].Alignment = Alignment; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1018 | return i; |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 1019 | } |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 1020 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1021 | Constants.push_back(MachineConstantPoolEntry(C, Alignment)); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 1022 | return Constants.size()-1; |
| 1023 | } |
| 1024 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1025 | unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, |
| 1026 | unsigned Alignment) { |
| 1027 | assert(Alignment && "Alignment must be specified!"); |
| 1028 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
Chad Rosier | 0c1eac8 | 2012-06-19 23:37:57 +0000 | [diff] [blame] | 1029 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1030 | // Check to see if we already have this constant. |
| 1031 | // |
| 1032 | // FIXME, this could be made much more efficient for large constant pools. |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1033 | int Idx = V->getExistingMachineCPValue(this, Alignment); |
Cameron Zwarich | 5567869 | 2011-02-22 08:54:30 +0000 | [diff] [blame] | 1034 | if (Idx != -1) { |
| 1035 | MachineCPVsSharingEntries.insert(V); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1036 | return (unsigned)Idx; |
Cameron Zwarich | 5567869 | 2011-02-22 08:54:30 +0000 | [diff] [blame] | 1037 | } |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 1038 | |
| 1039 | Constants.push_back(MachineConstantPoolEntry(V, Alignment)); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1040 | return Constants.size()-1; |
| 1041 | } |
| 1042 | |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 1043 | void MachineConstantPool::print(raw_ostream &OS) const { |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1044 | if (Constants.empty()) return; |
| 1045 | |
| 1046 | OS << "Constant Pool:\n"; |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 1047 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1048 | OS << " cp#" << i << ": "; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1049 | if (Constants[i].isMachineConstantPoolEntry()) |
| 1050 | Constants[i].Val.MachineCPVal->print(OS); |
| 1051 | else |
Chandler Carruth | 560e395 | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1052 | Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); |
Dan Gohman | 0ba90f3e | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1053 | OS << ", align=" << Constants[i].getAlignment(); |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 1054 | OS << "\n"; |
| 1055 | } |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1058 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1059 | LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 1060 | #endif |