5353#include " llvm/IR/Instruction.h"
5454#include " llvm/IR/Instructions.h"
5555#include " llvm/IR/IntrinsicInst.h"
56+ #include " llvm/IR/Intrinsics.h"
5657#include " llvm/IR/LLVMContext.h"
5758#include " llvm/IR/Metadata.h"
5859#include " llvm/IR/Module.h"
@@ -2831,6 +2832,7 @@ class AssemblyWriter {
28312832 SetVector<const Comdat *> Comdats;
28322833 bool IsForDebug;
28332834 bool ShouldPreserveUseListOrder;
2835+ bool PrettyPrintIntrinsicArgs;
28342836 UseListOrderMap UseListOrders;
28352837 SmallVector<StringRef, 8 > MDNames;
28362838 // / Synchronization scope names registered with LLVMContext.
@@ -2841,7 +2843,8 @@ class AssemblyWriter {
28412843 // / Construct an AssemblyWriter with an external SlotTracker
28422844 AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
28432845 AssemblyAnnotationWriter *AAW, bool IsForDebug,
2844- bool ShouldPreserveUseListOrder = false );
2846+ bool ShouldPreserveUseListOrder = false ,
2847+ bool PrettyPrintIntrinsicArgs = false );
28452848
28462849 AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac,
28472850 const ModuleSummaryIndex *Index, bool IsForDebug);
@@ -2930,10 +2933,12 @@ class AssemblyWriter {
29302933
29312934AssemblyWriter::AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac,
29322935 const Module *M, AssemblyAnnotationWriter *AAW,
2933- bool IsForDebug, bool ShouldPreserveUseListOrder)
2936+ bool IsForDebug, bool ShouldPreserveUseListOrder,
2937+ bool PrettyPrintIntrinsicArgs)
29342938 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
29352939 IsForDebug(IsForDebug),
2936- ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
2940+ ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
2941+ PrettyPrintIntrinsicArgs(PrettyPrintIntrinsicArgs) {
29372942 if (!TheModule)
29382943 return ;
29392944 for (const GlobalObject &GO : TheModule->global_objects ())
@@ -2944,7 +2949,8 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
29442949AssemblyWriter::AssemblyWriter (formatted_raw_ostream &o, SlotTracker &Mac,
29452950 const ModuleSummaryIndex *Index, bool IsForDebug)
29462951 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/* Module=*/ nullptr ),
2947- IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false ) {}
2952+ IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false ),
2953+ PrettyPrintIntrinsicArgs(false ) {}
29482954
29492955void AssemblyWriter::writeOperand (const Value *Operand, bool PrintType) {
29502956 if (!Operand) {
@@ -4561,12 +4567,38 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
45614567 Out << ' ' ;
45624568 writeOperand (Operand, false );
45634569 Out << ' (' ;
4570+ bool HasPrettyPrintedArgs = PrettyPrintIntrinsicArgs && isa<IntrinsicInst>(CI) &&
4571+ Intrinsic::hasPrettyPrintedArgs (CI->getIntrinsicID ());
4572+
45644573 ListSeparator LS;
4565- for (unsigned op = 0 , Eop = CI->arg_size (); op < Eop; ++op) {
4566- Out << LS;
4567- writeParamOperand (CI->getArgOperand (op), PAL.getParamAttrs (op));
4568- }
4574+ if (HasPrettyPrintedArgs) {
4575+ Function *CalledFunc = CI->getCalledFunction ();
4576+ auto PrintArgComment = [&](unsigned ArgNo) {
4577+ if (!CalledFunc->hasParamAttribute (ArgNo, Attribute::ImmArg))
4578+ return ;
4579+ const Constant *ConstArg = dyn_cast<Constant>(CI->getArgOperand (ArgNo));
4580+ if (!ConstArg)
4581+ return ;
4582+ std::string ArgComment;
4583+ raw_string_ostream ArgCommentStream (ArgComment);
4584+ Intrinsic::ID IID = CalledFunc->getIntrinsicID ();
4585+ Intrinsic::printImmArg (IID, ArgNo, ArgCommentStream, ConstArg);
4586+ if (ArgComment.empty ())
4587+ return ;
4588+ Out << " /* " << ArgComment << " */ " ;
4589+ };
45694590
4591+ for (unsigned ArgNo = 0 , NumArgs = CI->arg_size (); ArgNo < NumArgs; ++ArgNo) {
4592+ Out << LS;
4593+ PrintArgComment (ArgNo);
4594+ writeParamOperand (CI->getArgOperand (ArgNo), PAL.getParamAttrs (ArgNo));
4595+ }
4596+ } else {
4597+ for (unsigned op = 0 , Eop = CI->arg_size (); op < Eop; ++op) {
4598+ Out << LS;
4599+ writeParamOperand (CI->getArgOperand (op), PAL.getParamAttrs (op));
4600+ }
4601+ }
45704602 // Emit an ellipsis if this is a musttail call in a vararg function. This
45714603 // is only to aid readability, musttail calls forward varargs by default.
45724604 if (CI->isMustTailCall () && CI->getParent () &&
@@ -4991,12 +5023,14 @@ void AssemblyWriter::printUseLists(const Function *F) {
49915023
49925024void Function::print (raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
49935025 bool ShouldPreserveUseListOrder,
4994- bool IsForDebug) const {
5026+ bool IsForDebug,
5027+ bool PrettyPrintIntrinsicArgs) const {
49955028 SlotTracker SlotTable (this ->getParent ());
49965029 formatted_raw_ostream OS (ROS);
49975030 AssemblyWriter W (OS, SlotTable, this ->getParent (), AAW,
49985031 IsForDebug,
4999- ShouldPreserveUseListOrder);
5032+ ShouldPreserveUseListOrder,
5033+ PrettyPrintIntrinsicArgs);
50005034 W.printFunction (this );
50015035}
50025036
@@ -5012,11 +5046,11 @@ void BasicBlock::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
50125046}
50135047
50145048void Module::print (raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
5015- bool ShouldPreserveUseListOrder, bool IsForDebug) const {
5049+ bool ShouldPreserveUseListOrder, bool IsForDebug, bool PrettyPrintIntrinsicArgs ) const {
50165050 SlotTracker SlotTable (this );
50175051 formatted_raw_ostream OS (ROS);
50185052 AssemblyWriter W (OS, SlotTable, this , AAW, IsForDebug,
5019- ShouldPreserveUseListOrder);
5053+ ShouldPreserveUseListOrder, PrettyPrintIntrinsicArgs );
50205054 W.printModule (this );
50215055}
50225056
0 commit comments